Java学习Day17 IO 节点流 处理流 字符流 字节流
![请添加图片描述](https://i-blog.csdnimg.cn/blog_migrate/64538b59a2377d5e43edb5d64dd5742e.png)
节点流NodeStream
字节输入流 FileInputStream
public class FileInputStream_ {
public static void main(String[] args) {
}
@Test
public void readFile01() throws IOException {
String filePath = "g:\\hello.txt";
int readData = 0;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
while ((readData = fileInputStream.read()) != -1) {
System.out.print((char) readData);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
fileInputStream.close();
}
}
@Test
public void readFile02() throws IOException {
String filePath = "g:\\hello.txt";
int readData = 0;
byte[] buf = new byte[8];
int readLen = 0;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
while ((readLen = fileInputStream.read(buf)) != -1) {
System.out.print(new String(buf,0,readLen));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
fileInputStream.close();
}
}
}
字节输出流FileOutputStream
public class FileOutputStream_ {
public static void main(String[] args) {
}
@Test
public void writeFile() throws IOException {
FileOutputStream fileOutputStream = null;
String filePath = "g:\\a.txt";
try {
fileOutputStream = new FileOutputStream(filePath);
fileOutputStream.write('a');
String str ="hello,world";
fileOutputStream.write(str.getBytes());
fileOutputStream.write(str.getBytes(),0,str.length());
fileOutputStream.write(str.getBytes(),0,3);
} catch (IOException e) {
e.printStackTrace();
}finally {
fileOutputStream.close();
}
}
}
字符输入流
public class FileReader_ {
public static void main(String[] args) throws FileNotFoundException {
String filePath = "g:\\story.txt";
FileReader fileReader = null;
int data = 0;
try {
fileReader = new FileReader(filePath);
while ((data = fileReader.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileReader != null) {
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void readFile() {
String filePath = "g:\\story.txt";
FileReader fileReader = null;
char[] buf =new char[8];
int readLen = 0;
try {
fileReader = new FileReader(filePath);
while ((readLen = fileReader.read(buf)) != -1) {
System.out.print(new String(buf,0,readLen));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileReader != null) {
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字符输出流
public class FileWriter_ {
public static void main(String[] args) throws IOException {
String filePath = "g:\\note.txt";
FileWriter fileWriter = null;
char[] chars ={'a','b','c'};
try {
fileWriter = new FileWriter(filePath);
fileWriter.write('H');
fileWriter.write(chars);
fileWriter.write("御狐王".toString(),0,3);
fileWriter.write("北京欢迎你");
fileWriter.write("上海",0,2);
} catch (IOException e) {
e.printStackTrace();
} finally {
fileWriter.close();
}
}
}
Copy
public class FileCopy {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
String srcFilePath = "C:\\Users\\Administrator\\Desktop\\壁纸包\\1.png";
String copFilePath = "G:\\1.png";
try {
fileInputStream = new FileInputStream(srcFilePath);
fileOutputStream = new FileOutputStream(copFilePath);
byte[] buf = new byte[1024];
int readLen = 0;
while ((readLen = fileInputStream.read(buf)) != -1) {
fileOutputStream.write(buf, 0, readLen);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
fileInputStream.close();
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
处理流BufferedStream
字符输入流
public class BufferedReader_ {
public static void main(String[] args) throws IOException {
String filePath ="G:a.java";
BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
String line;
while ((line = bufferedReader.readLine())!=null)
{
System.out.println(line);
}
bufferedReader.close();
}
}
字符输出流
public class BufferedWriter_ {
public static void main(String[] args) throws IOException {
String filePath ="G:\\BufferedWriter.txt";
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath,true));
bufferedWriter.write("hello world\n");
bufferedWriter.write("hello world");
bufferedWriter.newLine();
bufferedWriter.write("hello world\n");
bufferedWriter.close();
}
}
Copy
public class BufferedCopy {
public static void main(String[] args) throws IOException {
String srcFilePath = "g:\\a.java";
String destFilePath = "g:\\a2.java";
BufferedReader br =null;
BufferedWriter bw =null;
br =new BufferedReader(new FileReader(srcFilePath));
bw =new BufferedWriter(new FileWriter(destFilePath));
String line;
while ((line =br.readLine())!=null){
bw.write(line);
bw.newLine();
}
System.out.println("Ok");
if (br != null) {
br.close();
}
if (bw != null) {
bw.close();
}
}
}
Copy2
public class BufferedCopy02 {
public static void main(String[] args) throws IOException {
String srcFilePath = "C:\\Users\\Administrator\\Desktop\\壁纸包\\1.png";
String destFilePath = "G:\\1.png";
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFilePath));
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destFilePath));
byte[] buff =new byte[1024];
int readLen =0;
while((readLen = bufferedInputStream.read(buff))!=-1){
bufferedOutputStream.write(buff,0,readLen);
}
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
if (bufferedOutputStream != null) {
bufferedOutputStream.close();
}
}
}