package Text; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class IO { public static void main(String[] args) throws IOException { long begin = System.currentTimeMillis(); SrSc2(); long end = System.currentTimeMillis() - begin; System.out.println("耗时:" + end + "毫秒"); } //挨个字节读取输入 private static void SrSc1() throws IOException { //判断文件是否存在 File a = new File("E:/Text.txt"); if(a.exists()) { FileInputStream fIn = new FileInputStream("E:\\Text.txt"); FileOutputStream fOut = new FileOutputStream("E:\\复制文件1.txt"); int temp = 0; while(temp != -1) { temp = fIn.read(); fOut.write(temp); } //关闭输入输出流 fIn.close(); fOut.close(); //打印读取成功信息 System.out.println("挨个字节复制文件成功"); } else { //判断文件如果不存在,打印提示信息 System.out.println("文件不存在"); } } //用数组批量复制 private static void SrSc2() throws IOException { //判断文件是否存在 File a = new File("E:/Text.txt"); if(a.exists()) { FileInputStream fIn = new FileInputStream("E:\\Text.txt"); FileOutputStream fOut = new FileOutputStream("E:\\复制文件2.txt"); //获取文件长度 int n = fIn.available(); //创建一个可以读取的数组 byte[] b = new byte[n]; fIn.read(b); //复制 fOut.write(b); //关闭输入输出流 fIn.close(); fOut.close(); //打印读取成功信息 System.out.println("批量字节复制成功"); } else { //判断文件如果不存在,打印提示信息 System.out.println("文件不存在"); } } }
package Text; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FileIO { public static void main(String[] args) throws IOException { long begin = System.currentTimeMillis(); Reade2(); long end = System.currentTimeMillis() - begin; System.out.println("耗时:" + end + "毫秒"); } private static void Write() throws IOException { //创建一个文件、实例化对象 FileOutputStream fo = new FileOutputStream("E:/Text.java"); //输入需要写入的文档内容 String str1 = "China中国188"; String str2 = "Name:伯乐在此-123"; String str3 = "这是个什么鬼"; //关联文档内容 byte[] b1 = str1.getBytes(); byte[] b2 = str2.getBytes(); byte[] b3 = new byte[100]; b3 = str3.getBytes(); //获取文件长度 int len1 = b1.length; int len2 = b2.length; int len3 = b3.length; //写入文档内容1 fo.write(str1.getBytes()); //写入文档内容2 int i2 = 0; fo.write(b2, i2, b2.length); //写入文档内容3 int i3 = 0; while(i3 < b3.length) { fo.write(b3[i3]); i3++; } //关闭 fo.close(); } private static void Reade1() throws IOException { //创建对象、关联需要读取的文件 FileInputStream fin1 = new FileInputStream("E:/Text.txt"); //获取文件长度 int n = fin1.available(); //创建一个可读取的数组 byte[] b = new byte[n]; fin1.read(b); //关闭 fin1.close(); //将字节数组转成字符串输出 System.out.println(new String(b)); } private static void Reade2() throws IOException { //创建对象、关联需要读取的文件 FileInputStream fin2 = new FileInputStream("E:/Text.txt"); //获取文本长度 File f = new File("E:/Text.txt"); int len = (int)f.length(); for(int i=0;i<len;i++) { int rDate = fin2.read(); if(-1 != rDate) { System.out.print((char)rDate); } } //关闭 fin2.close(); } }
                    
                
                
            
        
浙公网安备 33010602011771号