2013年11月19日星期二

java flow problems , seeking expert guidance

Requirements : receiving user input , write text files, read text files and displayed.
result of questions asked are:



Why do I format the output and the input is not the same when , how to make the output format and enter the same ?
My output is:


I wrote the program code is as follows:
Seek expert guidance ,

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Test1 {
public static void main(String [] args) throws IOException{
File f = new File("c:\\MyDoc","test.txt");
if(f.exists()){
System.out.println("文件已存在。覆盖内容");
}else{
System.out.println("文件不存在,创建新文件");
}
FileWriter fos = new FileWriter(f);
BufferedWriter bos = new BufferedWriter(fos);
Scanner input = new Scanner(System.in);
System.out.println("请输入文本内容,输入end结束输入:");

System.out.println("==========================");
boolean flag = true;
do{
String line = input.next();
if(line.equals("end")){
flag = true;
}else{
bos.write(line);
bos.flush();
bos.newLine();
flag = false;
}

}while(!flag);

System.out.println("输出文件内容:");
System.out.println("==========================");

FileReader fr = new FileReader("c:/MyDoc/test.txt");
BufferedReader br = new BufferedReader(fr);
String line1 ;
while((line1 = br.readLine())!= null){
System.out.println(line1); 
}



}
}


------ Solution ------------------------------------- -------
String line = input.next ();
changed
String line = input.nextLine (); on the line
------ Solution ------------------- -------------------------
String line = input.nextLine ();

没有评论:

发表评论