Java I/O stream-file input/output stream

FileInputStream and FileOutputStream classes

The FileInputStream and FileOutputStream classes are used to manipulate disk files. FileInputStream (file byte input stream), FileOutputStream (file byte output stream).
The FileInputStream and FileOutputStream classes can implement file reading and writing functions.

First create the file object

public class Study2 {
    
    

	public static void main(String[] args) {
    
    
		
		//创建文件对象
		File f = new File("word.txt");

	}
}

Create a FileOutputStream object to write information to the file

public class Study2 {
    
    

	public static void main(String[] args) {
    
    

		// 创建文件对象
		File f = new File("word.txt");

		// 将信息写入到文件中
		FileOutputStream out = null;
		try {
    
    
			out = new FileOutputStream(f);// 创建FileOutputStream对象
			byte b[] = "Holle Word!".getBytes();// 创建byte型数组
			out.write(b);// 将数组中的信息写入到文件中
		} catch (FileNotFoundException e) {
    
    
			e.printStackTrace();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			if (out != null) {
    
    // 判断out是否为空值,如果不等于空值,则需要将流关闭
				try {
    
    
					out.close();// 将流关闭
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
		}

	}
}

The word.txt file will appear in the project directory. Check in the file
Insert picture description here
and output the sentence in the byte array.
Insert picture description here
Create a FileInputStream object to read the information in the file

// 将文件信息读取输出
		FileInputStream in = null;
		try {
    
    
			in = new FileInputStream(f);// 创建FileInputStream对象
			byte b1[] = new byte[1024];//创建byte数组
			int len = in.read(b1);//从文件中读取信息
			System.out.println("文件中的信息是:" + new String(b1,0,len));
		} catch (FileNotFoundException e) {
    
    
			e.printStackTrace();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			if (in != null) {
    
    // 判断in是否为空值,如果不等于空值,则需要将流关闭
				try {
    
    
					in.close();// 将流关闭
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
		}

Insert picture description here
Complete code:

public class Study2 {
    
    

	public static void main(String[] args) {
    
    

		// 创建文件对象
		File f = new File("word.txt");

		// 将信息写入到文件中
		FileOutputStream out = null;
		try {
    
    
			out = new FileOutputStream(f);// 创建FileOutputStream对象
			byte b[] = "Holle Word!".getBytes();// 创建byte型数组
			out.write(b);// 将数组中的信息写入到文件中
		} catch (FileNotFoundException e) {
    
    
			e.printStackTrace();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			if (out != null) {
    
    // 判断out是否为空值,如果不等于空值,则需要将流关闭
				try {
    
    
					out.close();// 将流关闭
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
		}

		// 将文件信息读取输出
		FileInputStream in = null;
		try {
    
    
			in = new FileInputStream(f);// 创建FileInputStream对象
			byte b1[] = new byte[1024];// 创建byte数组
			int len = in.read(b1);// 从文件中读取信息
			System.out.println("文件中的信息是:" + new String(b1, 0, len));
		} catch (FileNotFoundException e) {
    
    
			e.printStackTrace();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			if (in != null) {
    
    // 判断in是否为空值,如果不等于空值,则需要将流关闭
				try {
    
    
					in.close();// 将流关闭
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
		}

	}
}

FileReader and FileWriter classes

Because the two classes FileInputStream and FileOutputStream only provide methods for reading bytes or byte arrays, Chinese characters occupy two bytes in bytes. If the reading is not good, garbled codes may occur, so there is FileReader (file character input stream) and FileWriter (file character output stream) character stream.
The two are similar and the methods are the same.
code show as below:

public class Study2 {
    
    

	public static void main(String[] args) {
    
    

		// 创建文件对象
		File f = new File("word.txt");

		// 将信息写入到文件中
		FileWriter out = null;
		try {
    
    
			out = new FileWriter(f);// 创建FileOutputStream对象
			String str = "Holle Word!";// 创建byte型数组
			out.write(str);// 将数组中的信息写入到文件中
		} catch (FileNotFoundException e) {
    
    
			e.printStackTrace();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			if (out != null) {
    
    // 判断out是否为空值,如果不等于空值,则需要将流关闭
				try {
    
    
					out.close();// 将流关闭
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
		}

		// 将文件信息读取输出
		FileReader in = null;
		try {
    
    
			in = new FileReader(f);// 创建FileInputStream对象
			char c[] = new char[1024];// 创建byte数组
			int len = in.read(c);// 从文件中读取信息
			System.out.println("文件中的信息是:" + new String(c, 0, len));
		} catch (FileNotFoundException e) {
    
    
			e.printStackTrace();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			if (in != null) {
    
    // 判断in是否为空值,如果不等于空值,则需要将流关闭
				try {
    
    
					in.close();// 将流关闭
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
		}

	}
}

Guess you like

Origin blog.csdn.net/javanofa/article/details/104443069