- 讲师:刘萍萍 / 谢楠
- 课时:160h
- 价格 4580 元
特色双名师解密新课程高频考点,送国家电网教材讲义,助力一次通关
配套通关班送国网在线题库一套
百度广告
用FileInputStream 来读取数据的常用方法是:
DataInputStream in = new DataInputStream(fstream);
完整代码见 Example 1。
用FileOutputStream 来写入数据的常用方法是:
PrintStream p = new PrintStream( out );
完整代码见 Example 2。
JDK1.1中的 InputStream & OutputStream 可以对文件或二进制文件进行操作。
用FileReader 来读取文件的常用方法是:
BufferedReader br = new BufferedReader(fr);
完整代码见 Example 3。
用 FileWriter 来写入文件的常用方法是:
PrintWriter out = new PrintWriter(fw);
入数据或会自动开一新行。写完后要记得 用out.close() 关闭输出,用fw.close() 关闭文件。
-------------------------------------------------------------- following is the source code of examples------------------------------------------------------
Example 1:
// Demonstrates FileInputStream and DataInputStream
class FileInputDemo {
// args.length is equivalent to argc in C
try {
FileInputStream fstream = new FileInputStream(args[0]);
DataInputStream in = new DataInputStream(fstream);
while (in.available() !=0) {
System.out.println (in.readLine());
in.close();
System.err.println("File input error");
}
System.out.println("Invalid parameters");
}
Example 2:
// Demonstration of FileOutputStream and PrintStream classes
class FileOutputDemo
public static void main(String args) {
PrintStream p; // declare a print stream object
try {
out = new FileOutputStream("myfile.txt");
p = new PrintStream( out );
p.close();
System.err.println ("Error writing to file");
}
Example 3:
// User FileReader in JDK1.1 to read a file
class FileReadTest {
FileReadTest t = new FileReadTest();
}
void readMyFile() {
int recCount = 0;
FileReader fr = new FileReader("mydata.txt");
record = new String();
recCount++;
}
fr.close();
System.out.println("Uh oh, got an IOException error!");
}
Example 4:
// User FileWriter in JDK1.1 to writer a file
class FileWriteTest {
FileWriteTest t = new FileWriteTest();
}
void WriteMyFile() {
FileWriter fw = new FileWriter("mydata.txt");
out.print("hi,this will be wirte into the file!");
fw.close();
System.out.println("Uh oh, got an IOException error!");
}
责编:罗莉
上一篇:怎样设置JSP的虚拟目录
下一篇:javamail在jsp中调用
课程专业名称 |
讲师 |
课时 |
查看课程 |
---|
课程专业名称 |
讲师 |
课时 |
查看课程 |
---|
点击加载更多评论>>