【每日日报】第三十五天---打印流
1 今天继续看书
打印流
1 package File;
2 import java.io.FileNotFoundException;
3 import java.io.FileOutputStream;
4 import java.io.PrintStream;
5
6 public class PrintStreamDemo {
7 public static void main(String[] args)throws FileNotFoundException{
8 PrintStream out=new PrintStream(new FileOutputStream("D:/Hello.txt"));
9 out.println(true);
10 out.println('A');
11 out.println(10.0);
12 out.println(12);
13 out.println("Hello");
14 Person p=new Person();
15 p.setAge(20);
16 p.setName("sky");
17 out.println(p);
18 out.close();
19 }
20 }
使用RandomAccessFile随机读写文件
1 package File;
2 import java.io.IOException;
3 import java.io.RandomAccessFile;
4
5 public class RandomAccessFileDemo {
6 public static void main(String[] args)throws IOException{
7 RandomAccessFile acf = new RandomAccessFile("D:/Hello.txt","rw");
8 byte[] b=("Hello Java!!").getBytes();
9 acf.write(b);
10 acf.seek(6);
11 System.out.println("pointre="+acf.getFilePointer());
12 byte[] b2="C++".getBytes();
13 acf.write(b2);
14 int len=-1;
15 byte[] buf=new byte[1024];
16 acf.seek(0);
17 System.out.println("pointer="+acf.getFilePointer());
18 while((len=acf.read(buf))!=-1){
19 String s=new String(buf,0,len);
20 System.out.println(s);
21 }
22 }
23 }
2 没遇到什么问题
3 明天继续看书写例题