1.二进制方式读取文件
import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.List; import java.io.IOException; import java.io.InputStream; import java.nio.file.NoSuchFileException; import java.nio.charset.Charset; class InputCompare{ public boolean compareFiles(Path path1,Path path2)//判断两个文件的字节数和文件是否相同 throws NoSuchFileException {//抛出错误类型 if(Files.notExists(path1)) {//判断传入的path1的路径下是否存在该文件,不是则抛出 //错误 throw new NoSuchFileException(path1.toString());//打印错误路径 } if(Files.notExists(path2)) {//判断传入的path2的路径下是否存在该文件,不是则抛出 //错误 throw new NoSuchFileException(path2.toString()); } try { if(Files.size(path1) !=Files.size(path2)) {//判断两个文件的字节数目是否 //相同 return false; } }catch(IOException e){//处理抛出的异常 e.printStackTrace(); } try (InputStream inputStream1 = Files.newInputStream( path1,StandardOpenOption.READ);//从path1路径下的文件中读取二进 //制数据 InputStream inputStream2 = Files.newInputStream( path2,StandardOpenOption.READ)//从path2路径下的文件中读取二进制 //数据 ){ int i1,i2; do {//在InputStream中有内部指针每次使用都会使指针指向下一个元素 i1 = inputStream1.read(); i2 = inputStream2.read(); if(i1!=i2) {//将文件的元素之间进行对比 有一个元素不相同就返回false return false; } }while(i1 !=-1);//所有元素被内部指针全部指完了时返回-1 通过这个退出循环 return true;//两个文件之间所有的元素都相同 }catch (IOException e) {//捕获Files.createDirectory()方法抛出的异常 return false;//有一定异常直接返回false 为了安全 } } } public class Easy { public static void main(String[] args) { Path path1 = Paths.get("D:\music\emq\emm.txt");//使用get方法创建Path的快捷方式 Path path2 = Paths.get("D:\music\emq\fqn.txt");//使用get方法创建Path的快捷方式 Path path3 = Paths.get("D:\music\emq\emm.txt");//使用get方法创建Path的快捷方式 InputCompare list1 = new InputCompare(); try { System.out.println(list1.compareFiles(path1, path2));//false System.out.println(list1.compareFiles(path1, path3));//true } catch (IOException e) {//捕获Files.createDirectory()方法抛出的异常 // TODO Auto-generated catch block e.printStackTrace(); } } }
2.二进制方式写入文件
示例 Copy
import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.List; import org.omg.CORBA.portable.OutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.file.NoSuchFileException; import java.nio.charset.Charset; class OutputStreamDemo1{ public void CopyputFiles(Path path1,Path path2)//判断两个文件的字节数和文件是否相同 throws NoSuchFileException {//抛出错误类型 if(Files.notExists(path1) || Files.notExists(path2)) {//判断传入的path1和path2的路径下是否存在该文 //件,不是则抛出错误 throw new NoSuchFileException(path1.toString()+path2.toString()); //打印错误路径 } byte[] readData = new byte[1024];//存储元素的数组 try (InputStream inputStream = Files.newInputStream( path1,StandardOpenOption.READ);//从path1路径下的文件中读取二进 //制数据 java.io.OutputStream outputStream = Files.newOutputStream( path2,StandardOpenOption.CREATE)//在对应路径下的文件写入二进制 ){ int i=inputStream.read(readData);//将文本的数据存储到数组里 while(i !=-1)//所有元素被内部指针全部指完了时返回-1 通过这个退出循环 { outputStream.write(readData,0,i);//在0开始的位置将1024个字节写入数 //据流 i = inputStream.read(readData); } }catch (IOException e) {//捕获Files.createDirectory()方法抛出的异常 e.printStackTrace(); } } } public class Easy { public static void main(String[] args) { Path path1 = Paths.get("D:\music\emq\emm.txt");//使用get方法创建Path的快捷方式 Path path2 = Paths.get("D:\music\emq\fqn.txt");//使用get方法创建Path的快捷方式 OutputStreamDemo1 list1 = new OutputStreamDemo1(); try { list1.CopyputFiles(path1, path2);//注意该方法在被复制文件的字节数大于源文件的字节 //数时无法将多于的字节删除 } catch (IOException e) {//捕获Files.createDirectory()方法抛出的异常 // TODO Auto-generated catch block e.printStackTrace(); } } }
(3)写入字符 方法1 FileWriter
public class Easy { public static void main(String[] args) { Path path1 = Paths.get("D:\music\emq\emm.txt");//使用get方法创建Path的快捷方式 String string = "你好"; Charset charset = Charset.forName("GB2312");//注意写入的格式 try(java.io.OutputStream outputStream = Files.newOutputStream(path1,StandardOpenOption.CREATE);//配置写入二进制数据的 //流 OutputStreamWriter writer = new OutputStreamWriter( outputStream,charset)) //配置写入的文件路径 和写入的格式 { writer.write(string);//写入你好 }catch (IOException e) {//捕获Files.createDirectory()方法抛出的异常 // TODO Auto-generated catch block e.printStackTrace(); } } }
2.写入 方法2 printWriter 一般使用这个,因为FileWriter必须使用计算机的编码来输出字符,这也就是说有些字符无法被正确的输出
public class Easy { public static void main(String[] args) { Path path1 = Paths.get("D:\music\emq\emm.txt");//使用get方法创建Path的快捷方式 String string = "Good,你好"; String string2 = "Easy,你好"; Charset charset = Charset.forName("GB2312");//注意写入的格式 try(BufferedWriter bufferedWriter =//配置一个默认大小的数据写入缓冲区 Files.newBufferedWriter( path1, charset, StandardOpenOption.CREATE);//配置文件路径 文件写入格式 写入 //模式 PrintWriter printWriter = new PrintWriter(bufferedWriter))//将配置好的缓冲区,交给pritfWriter对象 { printWriter.println(string);//在缓冲区输入Good,你好 printWriter.println(string2); }catch (IOException e) {//捕获Files.createDirectory()方法抛出的异常 // TODO Auto-generated catch block e.printStackTrace(); } } }
(4)读取字符 InputStreamReader
import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.Charset; public class Easy { public static void main(String[] args) { Path path1 = Paths.get("D:\music\emq\emm.txt");//使用get方法创建Path的快捷方式 Charset charset = Charset.forName("GB2312");//注意写入的格式 try(java.io.InputStream inputStream = Files.newInputStream( path1,StandardOpenOption.READ);//文本输入流中配置文件路径 和输入模式(读取文 //件) InputStreamReader read = new InputStreamReader( inputStream,charset)//让read对象在输入流中 *** 作 并且配置了写入的格式 ) { char[] Char = new char[27];//存储读取的字符 read.read(Char);//将返回的字符以数组的形式赋给对应的数组 for(char i : Char) { System.out.print(i);//遍历文件字符 } }catch (IOException e) {//捕获Files.createDirectory()方法抛出的异常 // TODO Auto-generated catch block e.printStackTrace(); } } }
(5)用缓冲区一次读取一行的字符串 BufferedReader
public class Easy { public static void main(String[] args) { Path path1 = Paths.get("D:\music\emq\emm.txt");//使用get方法创建Path的快捷方式 Charset charset = Charset.forName("GB2312");//注意写入的格式 BufferedReader readline = null; try { readline = Files.newBufferedReader(path1,charset);//配置缓存区 因为有可能抛出错 //误所以用try-catch模块 } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try{ String string = readline.readLine();//接收文本第一行的字符串 while(string != null) {//没有文本时退出循环 System.out.println(string);//遍历字符串 string=readline.readLine();//我估计应该是有内部指针 这一行指完,二次调用指下一 //行的字符串 } }catch (IOException e) {//捕获Files.createDirectory()方法抛出的异常 // TODO Auto-generated catch block e.printStackTrace(); } } }
(6)将控制台显示的信息转到文件 PrintStream
public class Easy { public static void main(String[] args) { Path path1 = Paths.get("D:\music\emq\emm.txt");//使用get方法创建Path的快捷方式 try(java.io.OutputStream outputStream = Files.newOutputStream( path1, StandardOpenOption.APPEND);//配置输出流 文件路径 打开文件的模式为追加 PrintStream print = new PrintStream(outputStream,true)//使用System.out.println();打印写入文件 ) { System.setOut(print);//更改System.out.println()方法的输出方式 System.out.println("World");//向文件写入World }catch (IOException e) {//捕获Files.createDirectory()方法抛出的异常 // TODO Auto-generated catch block e.printStackTrace(); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)