数据i/o

数据I/O流
文件输出流的应用。
定义如下字符串:
String str = “12345abcdef@#%&软件工程”;
编写程序将该字符串写入文件”data.txt”。
package homework;
import java.io.
;
public class IO {

public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
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();
}
}

文件输入流的应用。
修改第1题中的程序,读文件”data.txt”,将读到的数据输出在控制台。
package homework;
import java.io.*;
public class readFile {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String ss;
File f1=new File("C:\Users\WIN10\Desktop\data.txt");
FileReader fw=new FileReader(f1);
BufferedReader fy=new BufferedReader(fw);
while((ss=fy.readLine())!=null){
System.out.println(ss);
}
fy.close();
fw.close();
}

}

posted @ 2019-07-09 12:08  海诺音  阅读(181)  评论(0编辑  收藏  举报