導航:首頁 > 編程語言 > java輸入輸出

java輸入輸出

發布時間:2022-01-12 20:24:03

java中的輸入輸出流

首先,你問的應該是「在D:/aaa/aa.txt中輸出的為什麼不是4444ddd3333ccc2222bbb1111aaa,而是1111aaa啊」對吧?!你在命令行下面輸入的應該是4444ddd,3333ccc,2222bbb,1111aaa吧。

然後呢,說說為啥是1111aaa吧,因為sss = (String) it.next(); 這里不是sss += (String) it.next()。你只是把最後的1111aaa賦給了sss而已。所以要用+=,可是用了+=還是不行,因為sss的原值為null,所以輸出的時候還有帶有null字樣,所以你要給sss賦初值"",這樣就可以了。

② JAVA 輸入輸出

這個是我隨便寫的,功能都實現了
不過你可以拿去簡化一下,比如用switch case 之類 的
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class HttpClientTutorial{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
while(!str.equals("exit")){
for(int i=0;i<str.length();i++){
if(str.charAt(i)=='+'){
int left=Integer.parseInt(str.substring(0,i));
int right=Integer.parseInt(str.substring(i+1));
System.out.println(left+"+"+right+"="+(left+right));
}
if(str.charAt(i)=='-'){
int left=Integer.parseInt(str.substring(0,i));
int right=Integer.parseInt(str.substring(i+1));
System.out.println(left+"-"+right+"="+(left-right));
}
if(str.charAt(i)=='*'){
int left=Integer.parseInt(str.substring(0,i));
int right=Integer.parseInt(str.substring(i+1));
System.out.println(left+"*"+right+"="+(left*right));
}
if(str.charAt(i)=='/'){
int left=Integer.parseInt(str.substring(0,i));
int right=Integer.parseInt(str.substring(i+1));
System.out.println(left+"/"+right+"="+(left/right));
}
}
str=sc.nextLine();
}
}
}

③ java 輸入輸出

import java.util.Scanner;

public class Group {
public static void main(String arg[]) {
System.out.println("輸入一段英文:");
Scanner s = new Scanner(System.in);
String str = s.nextLine();
System.out.println(str);
}
}

④ java怎麼輸出

java控制台輸出由print( ) 和 println( )來完成最為簡單。這兩種方法由rintStream(System.out引用的對象類型)定義。盡管System.out是一個位元組流,用它作為簡單程序的輸出是可行的。因為PrintStream是從OutputStream派生的輸出流,它同樣實現低級方法write(),write()可用來向控制台寫數據。PrintStream 定義的write( )的最簡單的形式如下:

void write(int byteval)

該方法按照byteval指定的數目向文件寫位元組。盡管byteval 定義成整數,但只有低位的8個位元組被寫入。下面的短例用 write()向屏幕輸出字元「A」,然後是新的行。

// Demonstrate System.out.write().

class WriteDemo {

public static void main(String args[]) {

int b;

b = 'A';

System.out.write(b);

System.out.write(' ');

}

}

一般不常用write()來完成向控制台的輸出(盡管這樣做在某些場合非常有用),因為print()和println() 更容易用。

四、PrintWriter類

盡管Java允許用System.out向控制台寫數據,但建議僅用在調試程序時或在常式中。對於實際的程序,Java推薦的向控制台寫數據的方法是用PrintWriter流。PrintWriter是基於字元的類。用基於字元類向控制台寫數據使程序更為國際化。PrintWriter定義了多個構造函數,這里所用到的一個如下:

PrintWriter(OutputStream outputStream, boolean flushOnNewline)

outputStream是OutputStream類的對象,flushOnNewline控制Java是否在println()方法被調用時刷新輸出流。如果flushOnNewline為true,刷新自動發生,若為false,則不發生。

PrintWriter支持所有類型(包括Object)的print( )和println( )方法,這樣,就可以像用ystem.out那樣用這些方法。如果遇到不同類型的情況,PrintWriter方法調用對象的toString()方法並列印結果。用PrintWriter向外設寫數據,指定輸出流為System.out並在每一新行後刷新流。例如這行代碼創建了與控制台輸出相連的PrintWriter類。

PrintWriter pw = new PrintWriter(System.out, true);

下面的應用程序說明了用PrintWriter處理控制台輸出的方法:

// Demonstrate PrintWriter

import java.io.*;

public class PrintWriterDemo {

public static void main(String args[]) {

PrintWriter pw = new PrintWriter(System.out, true);

pw.println("This is a string");

int i = -7;

pw.println(i);

double d = 4.5e-7;

pw.println(d);

}

}

該程序的輸出如下:

This is a string

-7

4.5E-7

⑤ java語言的輸入和輸出語法。

輸出System.out.print(""); 輸入有點小復雜,我舉個例子 首先引入import java.util.Scanner;包 Scanner scan=new Scanner(System.in); int i; if (scan.hasNextInt()) i = scan.nextInt(); System.out.println("你輸入的是" + i);

⑥ JAVA輸入輸出流操作

這個easy 您是要在控制台輸入的吧?
代碼:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;

public class Write {

public static void main(String[] args) throws Exception {
String path = "C:\\my.txt";
InputStreamReader isr = new InputStreamReader(System.in);// 鍵盤輸入
BufferedReader input = new BufferedReader(isr);
System.out.println("請輸入姓名,計算機成績,JAVA成績,每項以兩個空格分開:");
String text = input.readLine();
if (Writes(path, text)) {
System.out.println("寫入成功");
}
}

public static boolean Writes(String path, String text) throws Exception {
File file = new File(path);
;
FileWriter fileWriter = new FileWriter(file, true);// true表示以追加形式寫文件
System.out.println(text);
fileWriter.write(text);
fileWriter.close();
return true;
}
}

⑦ Java中如何實現文件的輸入和輸出

程序如下:
<span style="color:#990000;">

</span>File file1 = new File("/home/a123/a");

if (file1.exists()) {
System.out.println("存在文件夾a");
} else {
file1.mkdir(); // 文件夾的創建 創建文件夾/home/a123/a
}
File file2 = new File("/home/a123/a/test");
if (file2.exists()) {
System.out.println("存在文件夾或者文件test");
} else {
try {
file2.createNewFile(); // 文件的創建,注意與文件夾創建的區別
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* 最簡單的文件讀寫方法是使用類FileWriter
* (它的父類依次是java.io.OutputStreamWriter——>java.io.Writer——>java.lang.Object );
*/

// 下面是向文件file2裡面寫數據
try {
FileWriter fileWriter = new FileWriter(file2);
String s = new String("This is a test! \n" + "aaaa");
fileWriter.write(s);
fileWriter.close(); // 關閉數據流

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

/*
* 這樣寫數據的話,是完全更新文件test裡面的內容,即把以前的東西全部刪除,重新輸入。
* 如果不想刪除以前的數據,而是把新增加的內容增添在文件末尾處。只需要在創建FileWriter對象時候,使用另外一個構造函數即可:
* FileWriter fileWriter=new FileWriter(file2,true);
*/

// 下面是從文件file2讀東西
try {
FileReader fileReader = new FileReader(file2);
String s = null;
char ch;
try {
char[] c = new char[100];
fileReader.read(c,0,2); // 具體想得到文件裡面的什麼值(單個char?int?還是String?),
System.out.println(c);
fileReader.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* 具體想得到文件裡面的什麼值(單個char?int?還是String?),需要知道不通read的不同用法:
* 1. int read() 讀取單個字元。
* 2. int read(char[] cbuf) 將字元讀入數組。 可以再將字元型數組轉化位字元串
* 3. int read(char[] cbuf,int off,int len) 將字元讀入數組的某一部分。
* 這三個方法都返回一個int值,作用是:讀取的字元數,如果已到達流的末尾,則返回 -1.
*/

}

⑧ java中的輸入輸出

可以用System.in進行輸入,之後System.out.println進行輸出。
代碼舉例如下:
public class Test
{
public static void main(String args[])
{
int i,min,max;
int A[] = new int[10];
Scanner input=new Scanner(System.in);
for(int j=0,j<10;j++){//輸入10個數
System.out.println("請輸入一個數字:");
int length=input.nextInt();//輸入一個字元串
A[i] =length;
}
min=max=A[0];
System.out.print("數組A的元素包括:");
int j =0;
int n =0 ;
for(i=0;i<A.length;i++)
{
System.out.print(A[i]+" ");
if(A[i]>max) // 判斷最大值
j =i;
max=A[i];
if(A[i]<min) // 判斷最小值
min=A[i];
n =i
}
System.out.println("\n數組的最大值是:"+max+".數組的位置是:"+(j+1)); // 輸出最大值和最大值的位置
System.out.println("數組的最小值是:"+min+".數組的位置是:"+(n+1)); // 輸出最小值
}

⑨ JAVA輸入輸出的用法

你是說控制台輸入輸出的問題吧
代碼如下:
public static void main(String[] arg){
Scanner scanner = new Scanner(System.in);
System.out.print("請輸入內容,並按回車鍵提交:");
String str = scanner.next();//輸入
System.out.println("您輸入的是:"+str);//輸出
}

⑩ java怎麼樣實現字元串輸入輸出問題

代碼如下:

importjava.util.Scanner;

publicclassApp{

publicstaticvoidmain(String[]args){

Scannerscanner=newScanner(System.in);

char[]characters={
'Z','Y','X','W','V','U','T','S','R',
'Q','P','O','N','M','L','K','J','I',
'H','G','F','E','D','C','B','A'
};

Strings=scanner.nextLine();

Strings2="";

for(charch:s.toCharArray()){
if(Character.isUpperCase(ch)){
s2+=characters[ch-'A'];
}else{
s2+=ch;
}
}

System.out.println(s2);
}
}

運行結果:

閱讀全文

與java輸入輸出相關的資料

熱點內容
程序員謎語 瀏覽:525
央行下了死命令 瀏覽:687
電腦的文件夾怎麼管理 瀏覽:81
遺傳演算法vb 瀏覽:31
主機做伺服器什麼配置好 瀏覽:612
python在excel模板生成數據 瀏覽:47
數位分離並求和python 瀏覽:39
河池源碼出售最新行情 瀏覽:741
曉龍伺服器怎麼樣 瀏覽:321
androidwidget圖片 瀏覽:833
95壓縮比與汽油標號 瀏覽:752
演算法崗位需要學什麼專業研究生 瀏覽:669
銀行卡忘了怎麼登錄手機app 瀏覽:962
加密雙菠蘿帽流蘇掛件 瀏覽:886
雲伺服器後台編程技巧 瀏覽:997
python人工智慧搭建 瀏覽:250
安卓m6用什麼下載 瀏覽:1000
對程序員有偏見嗎 瀏覽:292
如何讓伺服器運行緩慢 瀏覽:238
黑馬程序員入學流程 瀏覽:448