Commit e2e6fe62 by lihui.wang

io

parent 1ca1cc83
...@@ -2,6 +2,7 @@ package cn.freemud.demo; ...@@ -2,6 +2,7 @@ package cn.freemud.demo;
import org.junit.Test; import org.junit.Test;
import java.io.*;
import java.util.*; import java.util.*;
public class HomeworkTests { public class HomeworkTests {
...@@ -63,4 +64,43 @@ public class HomeworkTests { ...@@ -63,4 +64,43 @@ public class HomeworkTests {
System.out.print(a+" "); System.out.print(a+" ");
} }
/**
* io 字节流
*/
@Test
public void ioDemo(){
File file = new File("D:\\a.text");
try {
OutputStream os = new FileOutputStream(file);
String ts ="hello world!";
byte[] data =ts.getBytes();
os.write(data);
} catch (FileNotFoundException e) {
System.out.println("找不到文件");
e.printStackTrace();
} catch (IOException e) {
System.out.println("字节流输入文件错误");
e.printStackTrace();
}
}
/**
* 字符流
*/
@Test
public void ioDemo1(){
File file = new File("D:\\b.text");
try {
FileWriter fs = new FileWriter(file);
String ts ="hello world,b.text!";
fs.write(ts);
/**
* 字符流的文件会存入到缓冲池里,要随用随关闭
*/
fs.close();
} catch (IOException e) {
System.out.println("字符流输入文件错误");
e.printStackTrace();
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment