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 读写文件示例的更多相关文章

  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. GitHub预览网页[2019最新]

    GitHub预览网页 1. 创建仓库 2. 设置页面预览 3. 上传html 4. 访问网页 1. 创建仓库 登陆GitHub创建仓库 datamoko 添加基本信息: 仓库名.仓库描述,然后点击创建 ...

  2. 纯 CSS 画 iphone

    好几天没有更新了,直接上效果吧,哈哈!(我想这个应该大部分都会!哈哈哈!) 代码如下: html: <div class="container"> <div cl ...

  3. 安卓开发之常见Handler API和 定时器的使用

    package com.lidaochen.test; import android.os.Bundle; import android.os.Handler; import android.supp ...

  4. 分布式系统session一致性解决方案

    在单机系统中,不存在Session共享问题,但是在分布式系统中,我们必须实现session共享机制,使得多台应用服务器之间会话统一,如果不进行Session共享会出现数据不一致,比如:会导致请求落到不 ...

  5. python中的debug

    python中有很多的debug方法,大部分新人忽略了Python debugger(pdb)的重要性. 1.命令行运行 在终端中输入命令行   python -m pdb helloword.py ...

  6. zabbix Server 4.0 监控TCP的12种状态

      zabbix Server 4.0 监控TCP的12种状态 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 大家对TCP三次握手比较熟悉了,都知道当发生DOSS攻击时,客户端发送 ...

  7. 通过Nginx实现一个简单的网站维护通知页面

    原文:https://www.zhyd.me/article/106 在网站发版时,总会有那么一小段时间服务是访问不通的,一般用户看到的都会是一个502的错误页面 那么可以通过nginx实现一个简单的 ...

  8. CentOS7怎样安装Jenkins

    参考 http://pkg.jenkins-ci.org/redhat/ wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org ...

  9. java基础(9)---静态方法和成员方法

    一.方法:  方法的区别: 静态方法:有static方法 成员方法:没有static方法 方法的定义:  方法的调用:类.静态方法,对象.成员方法 一个MyClass类包含静态方法和成员方法: 静态方 ...

  10. Union-Find(并查集): Union-Find Application

    Union-find 可以应用在很多方面 之前我们看到了union-find在dynamic connectivity上的应用,接下来介绍它在percolation上的应用. union-find在K ...