最近在写论文,要用java进行文件的读写。记一下如何实现的。
FileOutputStream fout;
try {
fout = new FileOutputStream("CCC.txt",true);
PrintWriter pwr = new PrintWriter(fout);
pwr.print(iter);
pwr.print(" ");
pwr.print(masterproblem.lastObjValue);
pwr.print('\n');
pwr.close();
fout.flush();
} catch (FileNotFoundException e) {
System.out.println("error1");
} catch (IOException e) {
e.printStackTrace();
}
上面是向文件中写,下面是从文件里读数字
try {
Scanner in = new Scanner(new FileReader("CCC.txt"));
while(in.hasNextInt()){
int b = in.nextInt();
double a=in.nextDouble();
String c = String.valueOf(b);
System.out.println(a + "+" + c);
defaultcategorydataset.addValue(a, "First", c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}