学会了字节流拷备,能否复制所有文档?1216
- 内容介绍
- 文章标签
- 相关推荐
本文共计241个文字,预计阅读时间需要1分钟。
javaimport java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;
public class Test2 { public static void main(String[] args) { // 获取文件输入流 FileInputStream fileIn=null; try { fileIn=new FileInputStream(input.txt); // 读取文件内容 int data; while ((data=fileIn.read()) !=-1) { System.out.print((char) data); } } catch (FileNotFoundException e) { System.out.println(文件未找到); } catch (IOException e) { System.out.println(读取文件时发生错误); } finally { try { if (fileIn !=null) { fileIn.close(); } } catch (IOException e) { System.out.println(关闭文件流时发生错误); } } }}
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class Test2 { public static void main(String[] args) throws IOException { // 获取字节输入流 FileInputStream xsRead = new FileInputStream("小说.txt"); // 获取字节数组 byte[] bytes = new byte[200]; // 获取字节输出流 FileOutputStream xsWrite = new FileOutputStream("小说_字节_拷备.txt"); // 遍历读字节 int len = xsRead.read(bytes); while (len != -1) { // 代码来到这里 意味羞容器中有数据 // 立马写数据 xsWrite.write(bytes, 0, len); // 游戏移动 len = xsRead.read(bytes); } // 关资源 xsWrite.close(); xsRead.close(); // 程序结束 System.out.println("game over"); }}本文共计241个文字,预计阅读时间需要1分钟。
javaimport java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;
public class Test2 { public static void main(String[] args) { // 获取文件输入流 FileInputStream fileIn=null; try { fileIn=new FileInputStream(input.txt); // 读取文件内容 int data; while ((data=fileIn.read()) !=-1) { System.out.print((char) data); } } catch (FileNotFoundException e) { System.out.println(文件未找到); } catch (IOException e) { System.out.println(读取文件时发生错误); } finally { try { if (fileIn !=null) { fileIn.close(); } } catch (IOException e) { System.out.println(关闭文件流时发生错误); } } }}
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class Test2 { public static void main(String[] args) throws IOException { // 获取字节输入流 FileInputStream xsRead = new FileInputStream("小说.txt"); // 获取字节数组 byte[] bytes = new byte[200]; // 获取字节输出流 FileOutputStream xsWrite = new FileOutputStream("小说_字节_拷备.txt"); // 遍历读字节 int len = xsRead.read(bytes); while (len != -1) { // 代码来到这里 意味羞容器中有数据 // 立马写数据 xsWrite.write(bytes, 0, len); // 游戏移动 len = xsRead.read(bytes); } // 关资源 xsWrite.close(); xsRead.close(); // 程序结束 System.out.println("game over"); }}
