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 ...
随机推荐
- cxx11emu.h 和 logprint.h
cxx11emu.h 和 logprint.h /* Start of cxx11emu.h */ #ifndef STDBP_CXX11EMU_H_ #define STDBP_CXX11EMU_H ...
- 超详细Vue实现导航栏绑定内容锚点+滚动动画+vue-router(hash模式可用)
超详细Vue实现导航栏绑定内容锚点+滚动动画+vue-router(hash模式可用) 转载自:https://www.jianshu.com/p/2ad8c8b5bf75 亲测有效~ <tem ...
- CentOS7.5 部署Ceph luminous
环境 两台CentOS7.5,每台各两块硬盘部署OSD public network = 10.0.0.0/24 cluster network = 172.16.0.0/24 导入ceph的rpm ...
- 关于maven中版本控制问题
之前我们说过Maven的版本分为快照和稳定版本,快照版本使用在开发的过程中,方便于团队内部交流学习.而所说的稳定版本,理想状态下是项目到了某个比较稳定的状态,这个稳定包含了源代码和构建都要稳定. ma ...
- 【转】CCS5.5从硬盘读入.dat数据格式的单张图像
首页 博客 学院 CSDN学院 下载 论坛 APP CSDN 问答 商城 活动 VIP会员 ...
- mybatis中对数据表操作的四种语法
查询数据(select) select...from...where... 如:查询学生表中学号为某数的学生的全部信息 select * from stu where id=#{id} 增加数据(in ...
- Windows下学习C语言有哪些集成开发软件?
前言 初学者学习C语言遇到的最大困难想必就是搭建环境了,相当多的初学者就是被搭建环境导致放弃了学习编程,就我自己的经验而言,初学编程不应该受限于环境,使用成熟好用的环境就可以了,之后熟悉一些可以在慢慢 ...
- 关于RGBDSLAMV2学习、安装、调试过程
Step1:https://github.com/felixendres/rgbdslam_v2/wiki/Instructions-for-Compiling-Rgbdslam-(V2)-on-a- ...
- 本地安装部署ActiveCollab
ActiveCollab是一个非常易于使用.基于Web.开源的协作开发与项目管理工具. 我们公司一直在用这款工具,进行任务分配和时间填写,十分简便 ActiveCollab可以利用它轻松地搭建一个包括 ...
- [Python] Codecombat 攻略 地牢 Kithgard (1-22关)
首页:https://cn.codecombat.com/play语言:Python 第一界面:地牢 Kithgard(22关) 时间:1-3小时 内容:语法.方法.参数.字符串.循环.变量等 网页: ...