java文件流相关记录

     一个流可以定义为一个数据的序列。java中分为字节流和字符流,字节流是最基本的,主要在处理二进制数据,它是按字节处理得。但是实际过程中很多的数据都是文本,所以又提出了字符流的概念,它是按虚拟机的encode来处理,也就是要进行字符集的转化
这两个之间通过 InputStreamReader,OutputStreamWriter来关联。

	下面是字符流字节流之间的关系:
					   FilterOutputStream
		OutputStream(最基本的输写字节流) { Fileoutputstream
字节流:{								   ByteArrayOutputStream
					
					   ByteArrayInputStream 	
		InputStream(最基本的读取字节流)   { FilterInputStream
									   FileInputStream
									   StringBufferInputStream  //这个不推荐使用了
									   SequenceInputStream

					   BufferedWriter
		Writer(最基本的书写字符流)       	  OutputStreamWriter   FileWriter
字符流:{
 
 	   Read(最基本的读取字符流)		  BufferedReader
												    InputStreamReader     FileRead
     
 
    	    下面是字节流读取文本的例子
	File file = new File("C:/test.txt");
	if(file.exists()){
		InputStream inputStream = new FileInputStream(file);//字节流
		InputStreamReader reader = new InputStreamReader(inputStream);//字节流转为字符流   字符流也可以read() 转成 char
		BufferedReader bufferedReader = new BufferedReader(reader);//利用buff缓冲流 文件多的时候效率更快
		while (bufferedReader.ready()) {
			System.out.println(bufferedReader.readLine());
		} 
		//按打开顺序关闭流
	}
	字符流读取文本则更简单方便
	FileReader fileReader = new FileReader(file);
	int i;
	while ((i = fileReader.read()) != -1) {
		System.out.print((char) i);
	}
	//关闭流
	
 
	
	书写类似
	FileWriter fileWriter = new FileWriter(file);
	fileWriter.write("hello !");
	fileWriter.close();
		




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Java 中使用 I/O 复制文件可以使用如下的代码: ``` import java.io.*; public class CopyFile { public static void main(String[] args) { // 要复制的文件的路径 String srcFile = "/path/to/src/file.txt"; // 复制后的文件的路径 String destFile = "/path/to/dest/file.txt"; try (InputStream in = new FileInputStream(srcFile); OutputStream out = new FileOutputStream(destFile)) { // 缓冲区大小 byte[] buffer = new byte[1024]; int length; // 循环读取文件内容并写入到新文件中 while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } } catch (IOException e) { e.printStackTrace(); } } } ``` 上面的代码使用了 try-with-resources 语句,这样在结束后会自动关闭。如果不使用 try-with-resources 语句,需要在 finally 块中手动关闭。 需要注意的是,上面的代码只能复制纯文本文件。如果要复制二进制文件,需要使用字节(InputStream 和 OutputStream)而不是字符(Reader 和 Writer)。 ### 回答2: 文件复制通过使用Java中的I/O可以轻松地实现。下面是一个简单的示例,演示了如何使用I/O来实现文件复制。 ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileCopy { public static void main(String[] args) { String sourceFilePath = "source.txt"; // 源文件路径 String targetFilePath = "target.txt"; // 目标文件路径 try { // 创建输入和输出 FileInputStream input = new FileInputStream(sourceFilePath); FileOutputStream output = new FileOutputStream(targetFilePath); // 创建一个缓冲区,用于存储读取/写入的数据 byte[] buffer = new byte[1024]; int bytesRead; // 读取源文件中的数据,并将其写入目标文件中 while ((bytesRead = input.read(buffer)) != -1) { output.write(buffer, 0, bytesRead); } // 关闭 input.close(); output.close(); System.out.println("文件复制完成!"); } catch (IOException e) { System.out.println("发生错误:" + e.getMessage()); } } } ``` 上述代码中,我们首先指定源文件和目标文件的路径。然后,我们使用`FileInputStream`和`FileOutputStream`类创建输入和输出。我们还创建了一个缓冲区,用于存储读取和写入的数据。 在`while`循环中,我们使用`read()`方法从输入读取数据,并使用`write()`方法将读取的数据写入输出。我们还使用`bytesRead`变量来记录实际读取的字节数,以便正确写入到输出中。 最后,我们在`IOException`中捕获任何可能发生的错误,并将错误消息输出到控制台。如果没有错误发生,则输出"文件复制完成!"的消息。 通过运行上述代码,我们可以将一个文件的内容复制到另一个文件中。请确保源文件存在,并且目标文件不存在,以避免发生意外的覆盖。 ### 回答3: Java使用I/O完成文件复制非常简单。我们可以使用字节或字符来实现文件复制。 使用字节实现文件复制时,首先需要创建一个文件输入和一个文件输出。然后,通过循环从文件输入中读取字节,并将其写入文件输出中,实现文件复制。最后记得关闭输入和输出。 代码示例: ```java import java.io.*; public class FileCopy { public static void main(String[] args) { // 设置输入文件和输出文件的路径 String inputFilePath = "input.txt"; String outputFilePath = "output.txt"; try { FileInputStream inputStream = new FileInputStream(inputFilePath); FileOutputStream outputStream = new FileOutputStream(outputFilePath); // 定义一个字节数组来存储读取的字节数据 byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } // 关闭输入和输出 inputStream.close(); outputStream.close(); System.out.println("文件复制成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 使用字符实现文件复制时,操作步骤与字节类似。只是在创建时需要使用Reader和Writer类,同时要保证源文件和目标文件的编码类型一致。 代码示例: ```java import java.io.*; public class FileCopy { public static void main(String[] args) { // 设置输入文件和输出文件的路径 String inputFilePath = "input.txt"; String outputFilePath = "output.txt"; try { FileReader reader = new FileReader(inputFilePath); FileWriter writer = new FileWriter(outputFilePath); // 定义一个字符数组来存储读取的字符数据 char[] buffer = new char[1024]; int charsRead; while ((charsRead = reader.read(buffer)) != -1) { writer.write(buffer, 0, charsRead); } // 关闭输入和输出 reader.close(); writer.close(); System.out.println("文件复制成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 无论使用字节还是字符Java中的I/O都提供了方便的方法来实现文件复制。通过以上的代码示例,我们可以轻松地完成文件复制操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值