Java 读写文件示例
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- public class Test4 {
- public static void main(String[] args) {
- FileUtil f = new FileUtil();
- System.out.println(f.read("c:/a.txt"));
- final String fileName = "c:/a.txt";
- System.out.println(f.delete(fileName));
- System.out.println(f.write(fileName, "这是java写入的内容1"));
- System.out.println(f.append(fileName, "这是java写入的内容2"));
- System.out.println(f.write(fileName, "这是java写入的内容3"));
- }
- }
- /**
- * 文件读写类
- */
- class FileUtil {
- /*
- * 删除文件
- */
- public boolean delete(String fileName) {
- boolean result = false;
- File f = new File(fileName);
- if (f.exists()) {
- try {
- result = f.delete();
- } catch (Exception e) {
- e.printStackTrace();
- }
- } else {
- result = true;
- }
- return result;
- }
- /*
- * 读取文件
- */
- public String read(String fileName) {
- File f = new File(fileName);
- if (!f.exists()) {
- return "File not found!";
- }
- FileInputStream fs;
- String result = null;
- try {
- fs = new FileInputStream(f);
- byte[] b = new byte[fs.available()];
- fs.read(b);
- fs.close();
- result = new String(b);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result;
- }
- /*
- *写文件
- */
- public boolean write(String fileName, String fileContent) {
- boolean result = false;
- File f = new File(fileName);
- try {
- FileOutputStream fs = new FileOutputStream(f);
- byte[] b = fileContent.getBytes();
- fs.write(b);
- fs.flush();
- fs.close();
- result = true;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result;
- }
- /*
- * 追加内容到文件
- */
- public boolean append(String fileName, String fileContent) {
- boolean result = false;
- File f = new File(fileName);
- try {
- if (f.exists()) {
- FileInputStream fsIn = new FileInputStream(f);
- byte[] bIn = new byte[fsIn.available()];
- fsIn.read(bIn);
- String oldFileContent = new String(bIn);
- //System.out.println("旧内容:" + oldFileContent);
- fsIn.close();
- if (!oldFileContent.equalsIgnoreCase("")) {
- fileContent = oldFileContent + "\r\n" + fileContent;
- //System.out.println("新内容:" + fileContent);
- }
- }
- FileOutputStream fs = new FileOutputStream(f);
- byte[] b = fileContent.getBytes();
- fs.write(b);
- fs.flush();
- fs.close();
- result = true;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result;
- }
- }
- import java.io.*;
- public class Test4 {
- /**
- * 读取文件写入到另外一个文件
- * @param args
- */
- public static void main(String[] args) {
- BufferedReader buffReader = null;
- BufferedWriter buffWriter = null;
- try {
- buffReader = new BufferedReader(new FileReader("C:\\a.txt"));
- buffWriter = new BufferedWriter(new FileWriter("C:\\a_bak.txt"));
- String line = null;
- while ((line = buffReader.readLine()) != null) {
- buffWriter.write(line);
- buffWriter.newLine();
- buffWriter.flush();
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Java 读写文件示例的更多相关文章
- Java读写文件方法总结
Java读写文件方法总结 Java的读写文件方法在工作中相信有很多的用处的,本人在之前包括现在都在使用Java的读写文件方法来处理数据方面的输入输出,确实很方便.奈何我的记性实在是叫人着急,很多时候既 ...
- Java读写文件的几种方式
自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行 ...
- java读写文件大全
java读写文件大全 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(char[] ch,int o ...
- 【java】java 读写文件
场景:JDK8 将上传的文件,保存到服务器 Java读写文件操作: MultipartFile file InputStream inputStream = file.getInputStream( ...
- 转:Java读写文件各种方法及性能比较
干Java这么久,一直在做WEB相关的项目,一些基础类差不多都已经忘记.经常想得捡起,但总是因为一些原因,不能如愿. 其实不是没有时间,只是有些时候疲于总结,今得空,下定决心将丢掉的都给捡起来. 文件 ...
- Java读写文件常用方法
一.字符流:读写纯文本(txt,csv等), 1 字符流写文件主要用:FileWriter,BufferedWriter,PrintWriter 1.1 测试 FileWriter 写入 privat ...
- 161012、JAVA读写文件,如何避免中文乱码
1.JAVA读取文件,避免中文乱码. /** * 读取文件内容 * * @param filePathAndName * String 如 c:\\1.txt 绝对路径 * @return boole ...
- java 读写文件乱码问题
这样写,会出现乱码.原因是文件时gbk格式的, BufferedReader br = new BufferedReader(new FileReader(indir)); BufferedWrite ...
- java读写文件小心缓存数组
一般我们读写文件的时候都是这么写的,看着没问题哈. public static void main(String[] args) throws Exception { FileInputStr ...
随机推荐
- 6:Partial Update 内部原理 和 乐观锁并发控制
Partial Update 内部执行过程: 首先,ES文档是不可变的,它们只能被修改,不能被替换.Update Api 也不例外. Update API 简单使用与之前描述相同的 检索-修改-重建索 ...
- 搭建SpriBoot开发环境
一.搭建springboot开发环境 需求:使用springboot搭建一个项目,编写一个controller控制器,使用浏览器正常访问 springboot1.x版本--> 基于sprin ...
- 如何通过Restful API的方式读取SAP Commerce Cloud的Product Reference
从SAP官网上找到api的说明: https://api.sap.com/api/commerce_services/resource api endpoint: /rest/v2/electroni ...
- js求对象数组的交集/并集/差集/去重
1.求交集 var arr1 = [{name:'name1',id:1},{name:'name2',id:2},{name:'name3',id:3}]; var arr1Id = [1,2,3] ...
- MySQL Connection--使用tcpkill杀掉MySQL活跃连接
当MySQL连接被打满,连管理员也无法本地登录时,可以考虑使用tcpkill杀掉一些应用服务器创建的连接. CentOS 6安装tcpkill rpm安装包: libnids-1.24-1.el6.x ...
- mongdb插入 查询时间
Robo 3T mongdb客户端 https://www.robomongo.org/ 模糊查找关键字 db.getCollection('test').find({"name" ...
- 怎么查看二进制文件内容?linux下nm命令告诉你!
linux下强大的文件分析工具 -- nm 什么是nm nm命令是linux下自带的特定文件分析工具,一般用来检查分析二进制文件.库文件.可执行文件中的符号表,返回二进制文件中各段的信息. 目标文件. ...
- python网络-HTTP协议(28)
一.服务器和客户端介绍 1.什么是服务器? 简而言之:提供服务的机器就是服务器,至于提供什么服务不重要,重要的是要给其他人提供服务.例如:FTP服务器可以提供文件下载,SMTP服务器等等,不同的服务器 ...
- 《Exceptioning团队》第四次作业:项目需求调研与分析
一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 1.探索团队软件项目需求获取技巧与方法2.学会 ...
- Oracle 按指定顺序排序
select * from (select 'Nick' as item from dual union all select 'Viki' as item from dual union all s ...