Fluxo de entrada / saída do arquivo de fluxo de E / S Java

Classes FileInputStream e FileOutputStream

As classes FileInputStream e FileOutputStream são usadas para manipular arquivos de disco. FileInputStream (fluxo de entrada de byte de arquivo), FileOutputStream (fluxo de saída de byte de arquivo).
As classes FileInputStream e FileOutputStream podem implementar funções de leitura e gravação de arquivos.

Primeiro crie o objeto de arquivo

public class Study2 {
    
    

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

	}
}

Crie um objeto FileOutputStream para gravar informações no arquivo

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();
				}
			}
		}

	}
}

O arquivo word.txt aparecerá no diretório do projeto. Faça check-in do arquivo
Insira a descrição da imagem aqui
e produza a frase na matriz de bytes.
Insira a descrição da imagem aqui
Crie um objeto FileInputStream para ler as informações no arquivo

// 将文件信息读取输出
		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();
				}
			}
		}

Insira a descrição da imagem aqui
Código completo:

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();
				}
			}
		}

	}
}

Classes FileReader e FileWriter

Como as duas classes FileInputStream e FileOutputStream fornecem apenas métodos para ler bytes ou matrizes de bytes, os caracteres chineses ocupam dois bytes em bytes. Se a leitura não for boa, podem ocorrer códigos distorcidos, portanto, FileReader (fluxo de entrada de caracteres de arquivo) e FileWriter (fluxo de saída de caracteres de arquivo) fluxo de caracteres.
Os dois são semelhantes e os métodos são os mesmos.
código mostrado abaixo:

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();
				}
			}
		}

	}
}

Acho que você gosta

Origin blog.csdn.net/javanofa/article/details/104443069
Recomendado
Clasificación