日期:2014-05-20  浏览次数:20839 次

编写一个Java Application程序,打印命令行输入的所有参数。无法输出,求指教
我这里是利用随机流读写,先全部输入到终端,再写入文件,然后读取。输入exit后全部输出到终端。但是我发现没法输出,不知道是哪里的问题,求指教、


import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;


public class INOUT {


public static void main(String[] args) {
RandomAccessFile inAndOut=null;
String str;
Scanner sc;
String str1;
try{
File file=new File("file.txt");
file.createNewFile();
inAndOut=new RandomAccessFile("file.text","rw");
sc=new Scanner(System.in);
while(!(str=sc.nextLine()).equals("exit"))
{
inAndOut.writeChars(str);
inAndOut.writeChars("\n");
}

long position=0;
inAndOut.seek(0);
while( position<inAndOut.length()){

str1=inAndOut.readLine();

position =inAndOut.getFilePointer();
System.out.println(str1);
 
}
inAndOut.close();

}

catch(IOException e){}

}

}


------解决方案--------------------
File file=new File("file.txt");
inAndOut=new RandomAccessFile("file.text","rw");

写了2个不同的文件名。
输入到了file.text,而不是file.txt.

------解决方案--------------------
楼上正解
++