• JAVA 读取写入文件


    1. 读取 写入 复制 文件
    2. import java.io.*;
    3. public class text6 {
    4. public static void main(String[] args) {
    5. long s1=System.nanoTime();
    6. // System.out.println(fiel_read("file\\a.txt")); //读取文件
    7. //
    8. // String str="abcdefg123456你好中国";
    9. // file_write("file2","/a.txt",str); //写入文件
    10. //
    11. // System.out.println(file_copy("file\\1.md","file\\2.md")); //复制文件 原有文件 新文件
    12. //下面几个方法好用 已经封装好了
    13. String str="abcdefg123456你好中国";
    14. //file_write2("file","/e.txt",str); //写入文件
    15. file_write_coded("file","/m.txt",str,"GBK"); //带编码格式写入
    16. System.out.println(fiel_read_coded("file/m.txt","GBK")); //带编码读取
    17. System.out.println(fiel_read2("file/m.txt")); //读取
    18. long s2=System.nanoTime();
    19. System.out.println("耗时"+(s1-s2)/1E9+"秒");
    20. }
    21. public static String fiel_read2(String dirPath) { //读取
    22. StringBuffer retstr = new StringBuffer();
    23. try (
    24. BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(dirPath))); //dirPath
    25. ){
    26. while (true) {
    27. String s = br.readLine();
    28. if (s == null) {break;}
    29. retstr.append(s);
    30. }
    31. }catch (Exception e){
    32. System.out.println(e.toString());
    33. }
    34. return retstr.toString();
    35. }
    36. public static String fiel_read_coded(String dirPath,String coded) { //带编码读取
    37. if(coded==""){coded="UTF-8";}
    38. StringBuffer retstr = new StringBuffer();
    39. try (
    40. BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(dirPath),coded));
    41. ){
    42. while (true) {
    43. String s = br.readLine();
    44. if (s == null) {break;}
    45. retstr.append(s);
    46. }
    47. }catch (Exception e){
    48. System.out.println(e.toString());
    49. }
    50. return retstr.toString();
    51. }
    52. public static void file_write_coded(String dirPath,String name,String data,String coded) { //写入文件带编码写入
    53. if(coded==""){coded="UTF-8";}
    54. File file = new File(dirPath);
    55. if (!file.exists() && !file.isDirectory()) {file.mkdirs();}
    56. try (
    57. FileOutputStream fos=new FileOutputStream(dirPath+name,true);
    58. OutputStreamWriter osw=new OutputStreamWriter(fos,coded);
    59. PrintWriter pw=new PrintWriter(osw);
    60. ){
    61. pw.write(data);
    62. }catch (Exception e){
    63. System.out.println(e.toString());
    64. }
    65. }
    66. public static void file_write2(String dirPath,String name,String data) { //写入文件
    67. File file = new File(dirPath);
    68. if (!file.exists() && !file.isDirectory()) {file.mkdirs();}
    69. try (
    70. PrintWriter pw=new PrintWriter(new FileWriter(dirPath+name,true));
    71. ){
    72. // pw.print(5.5);
    73. // pw.println("一二三四五六");
    74. // pw.write("你好在吗?");
    75. pw.write(data);
    76. }catch (Exception e){
    77. System.out.println(e.toString());
    78. }
    79. }
    80. public static boolean file_copy(String file,String copyfile){ //复制文件 原有文件 新文件
    81. try (
    82. BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(copyfile)); //缓冲数据流 复制文件的路径
    83. BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file)); //缓冲数据流 被复制文件路径
    84. ){
    85. while (true){ //读取所有内容
    86. // int n=fis.read();
    87. // if(n==-1){break;}
    88. // fos.write(n);
    89. byte[] bs = new byte[1024];
    90. int n = bis.read(bs);
    91. if (n == -1) {break;}
    92. bos.write(bs);//将数组中的数据写入到目标文件
    93. }
    94. return true;
    95. }catch (Exception e){
    96. System.out.println(e.toString());
    97. }
    98. return false;
    99. }
    100. public static String fiel_read(String dirPath) { //读取文件
    101. StringBuffer retstr = new StringBuffer();
    102. try (BufferedInputStream bis=new BufferedInputStream(new FileInputStream(dirPath)); //缓冲数据流
    103. ) {
    104. while (true) {//利用read(byte[])+循环读取文件
    105. byte[] ss = new byte[1024];
    106. int n = bis.read(ss);
    107. if (n == -1) {
    108. break;
    109. }
    110. for (int i = 0; i < n; i++) {
    111. //System.out.println((char)ss[i]);
    112. retstr.append((char) ss[i]);
    113. }
    114. }
    115. //fis.close();
    116. } catch (Exception e) {
    117. System.out.println(e.toString());
    118. }
    119. return retstr.toString();
    120. }
    121. public static void file_write(String dirPath,String name,String data) { //写入文件
    122. File file = new File(dirPath);
    123. if (!file.exists() && !file.isDirectory()) {file.mkdirs();}
    124. try (BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(dirPath+name,true)); //缓冲数据流
    125. ){
    126. bos.write(data.getBytes());
    127. //fos.close();
    128. }catch (Exception e){
    129. System.out.println(e.toString());
    130. }
    131. }
    132. }

  • 相关阅读:
    一个简单利用WebGL绘制频谱瀑布图示例
    C# 命名空间和 using 指令详解
    [SDR] GNU Radio 系列教程(十四) —— GNU Radio 低阶到高阶用法的分水岭 ZMQ 的使用详解
    Kotlin高仿微信-第18篇-单聊-删除单条信息
    【linux】nmon 工具使用
    期货的含义及交易特点
    网上的搜索
    蚂蚁集团重组支付宝高层,井贤栋辞去支付宝法人和董事长职位
    Spring Boot
    设计模式:观察者模式(C++实现)
  • 原文地址:https://blog.csdn.net/webxscan/article/details/134333578