第二十一次学习Java
1、
package demo01;
import java.io.FileInputStream;
import java.io.IOException;
public class Demo01InputStream {
public static void main(String[] args) throws IOException {
FileInputStream fis= new FileInputStream (“D:\th\java\21\day21_code\a.txt”);
/*int len= fis.read();
System.out.println(len);
len= fis.read();
System.out.println(len);
len= fis.read();
System.out.println(len);
len= fis.read();
System.out.println(len);
len= fis.read();
System.out.println(len);
len= fis.read();
System.out.println(len);
len= fis.read();
System.out.println(len);
*/
int len=0;
while((len=fis.read())!=-1) {
System.out.println((char)len);
}
fis.close();
}
}
2、
package demo01;
import java.io.FileInputStream;
import java.io.IOException;
public class Demo02InputStream {
public static void main(String[] args) throws IOException {
FileInputStream fis= new FileInputStream("D:\\th\\java\\21\\day21_code\\a.txt");
/*
byte[] bytes=new byte[2];
int len=fis.read(bytes);
System.out.println("读取到的长度"+len);
//System.out.println(bytes[0]);//取出数组里面的值
//System.out.println(bytes[1]);
//System.out.println(Array.toString(bytes));
System.out.println(new String(bytes));
len=fis.read(bytes);
System.out.println("读取到的长度"+len);
System.out.println(new String(bytes));
len=fis.read(bytes);
System.out.println("读取到的长度"+len);
System.out.println(new String(bytes));
len=fis.read(bytes);
System.out.println("读取到的长度"+len);
System.out.println(new String(bytes));
len=fis.read(bytes);
System.out.println("读取到的长度"+len);
System.out.println(new String(bytes));*/
byte[] bytes=new byte[8];
int len=0;
while((len=fis.read(bytes))!=-1) {
System.out.println(len);
System.out.println(new String(bytes));
}
fis.close();
}
}
3、
package Demo02;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class Demo02Reader {
public static void main(String[] args) throws IOException {
FileReader fr= new FileReader("D:\\th\\Java\\21\\day21_code\\a.txt");
/*
int len=0;
while((len=fr.read())!=-1) {
System.out.println((char)len);
}*/
char[] cs=new char[9];
int len=0;
while((len=fr.read(cs))!=-1) {
System.out.println(len);
System.out.println(new String(cs));
}
}
}
4、
package Demo02;
import java.io.FileWriter;
import java.io.IOException;
public class Demo02Wreiter {
public static void main(String[] args) throws IOException {
FileWriter fw= new FileWriter(“D:\th\java\21\day21_code\a.txt”);
fw.write(100);
fw.flush();
fw.write(101);
fw.flush();
fw.write(102);
fw.flush();
fw.write(103);
fw.flush();
fw.close();
fw.write(103);
//fw.flush();
}
}