文件操作

it2022-05-05  156

package 文件操作; import java.io.*; import java.util.Arrays; public class File {  public static void main(String[] args) {             try {                 FileInputStream fin=new FileInputStream("D:\\文件操作\\a.txt");//创建文件字节输入流                 FileOutputStream fos=new FileOutputStream("D:\\文件操作\\b.txt");//创建文件字节输出流                 int len= fin.available();//获取文件长度                         byte[] arr = new byte[len];//创建数组用于读入数据                         while ((len = fin.read(arr)) != -1) {//读取1字节,字节流结束返回-1                             System.out.println("排序");                             Arrays.sort(arr);//对数组进行排序                             System.out.println("写入");                             fos.write(arr,0,len);//写入到输出流文件中                             fos.flush();                             fin.close();//关闭输入文件                             fos.close();//关闭写入文件                         }                     } catch (FileNotFoundException e) {//指定文件不存在                         e.printStackTrace();/*当try语句中出现异常是时,会执行catch.printStackTrace()方法的意思是:在命令行打印异常信息在程序中出错的位置及原因。*/                     } catch (IOException e) {//io异常                         e.printStackTrace();                     }  }                     }

转载于:https://www.cnblogs.com/baoyan/p/9250824.html


最新回复(0)