数据 I/O

1.文件输出流的应用。
  定义如下字符串:
  String str = “12345abcdef@#%&*软件工程”;
  编写程序将该字符串写入文件”data.txt”。

package homework;

import java.io.*;

public class IO {

public static void main(String[] args) throws IOException{

String ste="12345abcdef@#%&*软件工程";

File f1=new File("C:\Users\WIN10\Desktop\data.txt");

f1.createNewFile();

//System.out.println("名称"+f1.getName());

FileWriter fy=new FileWriter(f1);

BufferedWriter fw=new BufferedWriter(fy);

fw.write(ste);

fw.close();

fy.close();

}

}

2.文件输入流的应用。
   修改第1题中的程序,读文件”data.txt”,将读到的数据输出在控制台。

package homework;
import java.io.*;
public class readFile {
public static void main(String[] args) throws IOException {
String ss;
File f1=new File("C:UsersWIN10Desktopdata.txt");
FileReader fw=new FileReader(f1);
BufferedReader fy=new BufferedReader(fw);
while((ss=fy.readLine())!=null){
System.out.println(ss);
}
fy.close();
fw.close();
}

}

原文地址:https://www.cnblogs.com/ccqblog1127/p/11051374.html