1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4.  
  5. public class Test4 {
  6.  
  7. public static void main(String[] args) {
  8. FileUtil f = new FileUtil();
  9. System.out.println(f.read("c:/a.txt"));
  10. final String fileName = "c:/a.txt";
  11. System.out.println(f.delete(fileName));
  12. System.out.println(f.write(fileName, "这是java写入的内容1"));
  13. System.out.println(f.append(fileName, "这是java写入的内容2"));
  14. System.out.println(f.write(fileName, "这是java写入的内容3"));
  15.  
  16. }
  17. }
  18.  
  19. /**
  20. * 文件读写类
  21. */
  22. class FileUtil {
  23.  
  24. /*
  25. * 删除文件
  26. */
  27. public boolean delete(String fileName) {
  28. boolean result = false;
  29. File f = new File(fileName);
  30. if (f.exists()) {
  31. try {
  32. result = f.delete();
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36. } else {
  37. result = true;
  38. }
  39. return result;
  40. }
  41.  
  42. /*
  43. * 读取文件
  44. */
  45. public String read(String fileName) {
  46. File f = new File(fileName);
  47. if (!f.exists()) {
  48. return "File not found!";
  49. }
  50. FileInputStream fs;
  51. String result = null;
  52. try {
  53. fs = new FileInputStream(f);
  54. byte[] b = new byte[fs.available()];
  55. fs.read(b);
  56. fs.close();
  57. result = new String(b);
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61.  
  62. return result;
  63. }
  64.  
  65. /*
  66. *写文件
  67. */
  68. public boolean write(String fileName, String fileContent) {
  69. boolean result = false;
  70. File f = new File(fileName);
  71. try {
  72. FileOutputStream fs = new FileOutputStream(f);
  73. byte[] b = fileContent.getBytes();
  74. fs.write(b);
  75. fs.flush();
  76. fs.close();
  77. result = true;
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. }
  81. return result;
  82. }
  83.  
  84. /*
  85. * 追加内容到文件
  86. */
  87. public boolean append(String fileName, String fileContent) {
  88. boolean result = false;
  89. File f = new File(fileName);
  90. try {
  91. if (f.exists()) {
  92. FileInputStream fsIn = new FileInputStream(f);
  93. byte[] bIn = new byte[fsIn.available()];
  94. fsIn.read(bIn);
  95. String oldFileContent = new String(bIn);
  96. //System.out.println("旧内容:" + oldFileContent);
  97. fsIn.close();
  98. if (!oldFileContent.equalsIgnoreCase("")) {
  99. fileContent = oldFileContent + "\r\n" + fileContent;
  100. //System.out.println("新内容:" + fileContent);
  101. }
  102. }
  103.  
  104. FileOutputStream fs = new FileOutputStream(f);
  105. byte[] b = fileContent.getBytes();
  106. fs.write(b);
  107. fs.flush();
  108. fs.close();
  109. result = true;
  110. } catch (Exception e) {
  111. e.printStackTrace();
  112. }
  113. return result;
  114. }
  115.  
  116. }
  1. import java.io.*;
  2.  
  3. public class Test4 {
  4.  
  5. /**
  6. * 读取文件写入到另外一个文件
  7. * @param args
  8. */
  9. public static void main(String[] args) {
  10. BufferedReader buffReader = null;
  11. BufferedWriter buffWriter = null;
  12. try {
  13. buffReader = new BufferedReader(new FileReader("C:\\a.txt"));
  14. buffWriter = new BufferedWriter(new FileWriter("C:\\a_bak.txt"));
  15. String line = null;
  16. while ((line = buffReader.readLine()) != null) {
  17. buffWriter.write(line);
  18. buffWriter.newLine();
  19. buffWriter.flush();
  20. }
  21. } catch (FileNotFoundException e) {
  22. e.printStackTrace();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26.  
  27. }
  28. }

Java 读写文件示例的更多相关文章

  1. Java读写文件方法总结

    Java读写文件方法总结 Java的读写文件方法在工作中相信有很多的用处的,本人在之前包括现在都在使用Java的读写文件方法来处理数据方面的输入输出,确实很方便.奈何我的记性实在是叫人着急,很多时候既 ...

  2. Java读写文件的几种方式

    自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行 ...

  3. java读写文件大全

     java读写文件大全 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(char[] ch,int o ...

  4. 【java】java 读写文件

    场景:JDK8  将上传的文件,保存到服务器 Java读写文件操作: MultipartFile file InputStream inputStream = file.getInputStream( ...

  5. 转:Java读写文件各种方法及性能比较

    干Java这么久,一直在做WEB相关的项目,一些基础类差不多都已经忘记.经常想得捡起,但总是因为一些原因,不能如愿. 其实不是没有时间,只是有些时候疲于总结,今得空,下定决心将丢掉的都给捡起来. 文件 ...

  6. Java读写文件常用方法

    一.字符流:读写纯文本(txt,csv等), 1 字符流写文件主要用:FileWriter,BufferedWriter,PrintWriter 1.1 测试 FileWriter 写入 privat ...

  7. 161012、JAVA读写文件,如何避免中文乱码

    1.JAVA读取文件,避免中文乱码. /** * 读取文件内容 * * @param filePathAndName * String 如 c:\\1.txt 绝对路径 * @return boole ...

  8. java 读写文件乱码问题

    这样写,会出现乱码.原因是文件时gbk格式的, BufferedReader br = new BufferedReader(new FileReader(indir)); BufferedWrite ...

  9. java读写文件小心缓存数组

    一般我们读写文件的时候都是这么写的,看着没问题哈.   public static void main(String[] args) throws Exception {   FileInputStr ...

随机推荐

  1. 6:Partial Update 内部原理 和 乐观锁并发控制

    Partial Update 内部执行过程: 首先,ES文档是不可变的,它们只能被修改,不能被替换.Update Api 也不例外. Update API 简单使用与之前描述相同的 检索-修改-重建索 ...

  2. 搭建SpriBoot开发环境

      一.搭建springboot开发环境 需求:使用springboot搭建一个项目,编写一个controller控制器,使用浏览器正常访问 springboot1.x版本--> 基于sprin ...

  3. 如何通过Restful API的方式读取SAP Commerce Cloud的Product Reference

    从SAP官网上找到api的说明: https://api.sap.com/api/commerce_services/resource api endpoint: /rest/v2/electroni ...

  4. js求对象数组的交集/并集/差集/去重

    1.求交集 var arr1 = [{name:'name1',id:1},{name:'name2',id:2},{name:'name3',id:3}]; var arr1Id = [1,2,3] ...

  5. MySQL Connection--使用tcpkill杀掉MySQL活跃连接

    当MySQL连接被打满,连管理员也无法本地登录时,可以考虑使用tcpkill杀掉一些应用服务器创建的连接. CentOS 6安装tcpkill rpm安装包: libnids-1.24-1.el6.x ...

  6. mongdb插入 查询时间

    Robo 3T mongdb客户端 https://www.robomongo.org/ 模糊查找关键字 db.getCollection('test').find({"name" ...

  7. 怎么查看二进制文件内容?linux下nm命令告诉你!

    linux下强大的文件分析工具 -- nm 什么是nm nm命令是linux下自带的特定文件分析工具,一般用来检查分析二进制文件.库文件.可执行文件中的符号表,返回二进制文件中各段的信息. 目标文件. ...

  8. python网络-HTTP协议(28)

    一.服务器和客户端介绍 1.什么是服务器? 简而言之:提供服务的机器就是服务器,至于提供什么服务不重要,重要的是要给其他人提供服务.例如:FTP服务器可以提供文件下载,SMTP服务器等等,不同的服务器 ...

  9. 《Exceptioning团队》第四次作业:项目需求调研与分析

    一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 1.探索团队软件项目需求获取技巧与方法2.学会 ...

  10. Oracle 按指定顺序排序

    select * from (select 'Nick' as item from dual union all select 'Viki' as item from dual union all s ...