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. Keepalived简单理解

    Keepalived Keepalived是一个基于VRRP协议来实现的LVS服务高可用方案,可以利用其来避免单点故障.一个LVS服务会有2台服务器运行Keepalived,一台为主服务器(MASTE ...

  2. js 算法,数组排序

    冒泡排序.给数组按数字从小到大依次排序 arr = [1, 6, 7, 8, 9, 5, 18]; //最外层指针i从左到右依次循环指向,当最内层的指针循环一圈后,指针i才指向下个位置 //最内层指针 ...

  3. FreeRTOS计数型信号量

    API函数 //创建 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) #define xSemaphoreCreateCounting( uxMaxCount ...

  4. SetCurrentCellAddressCore 函数的可重入调用

    绑定数据在线程中 private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if (Di ...

  5. ClickHouse学习笔记

    1. 概述 ClickHouse是一个用于联机分析(OLAP:Online Analytical Processing)的列式数据库管理系统(DBMS:Database Management Syst ...

  6. linux系统多网卡热备实现高并发负载均衡

    #nmcli实现bonding #先停止NetworkManagerservice NetworkManager stop chkconfig NetworkManager off   //开机自启动 ...

  7. openstack各服务端口使用情况

    端口占用情况 端口情况可以使用ss -tanp命令进行查看 监听的所有端口ss -tanp | grep LISTEN 基础服务 22 --SSH 3306 --MariaDB(MySQL) 2701 ...

  8. 「CQOI2015」选数

    「CQOI2015」选数 题目描述 我们知道,从区间[L,H](L和H为整数)中选取N个整数,总共有(H-L+1)^N种方案.小z很好奇这样选出的数的最大公约数的规律,他决定对每种方案选出的N个整数都 ...

  9. MySQL复制介绍及搭建

    MySQL复制介绍 MySQL复制就是一台MySQL服务器(slave)从另一台MySQL服务器(master)进行日志的复制然后再解析日志并应用到自身,类似Oracle中的Data Guard. M ...

  10. 面向对象的继承和多态(is-a)

    继承的主要作用:我们使用继承主要是为了避免出现重复的定义,程序中如果出现多个地方有相同的定义,可以把相同程序提取出来定义为父类. 子类继承父类使用extends关键字,子类具备父类所有的属性和方法,包 ...