Java读写文件的几种方式
自工作以后好久没有整理Java的基础知识了。趁有时间,整理一下Java文件操作的几种方式。无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外。Java读写文件一般是通过字节、字符和行三种方式来进行文件的操作。
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader; public class FileUtil { /**
* 按行读取文件
*/
public static void ReadFileByLine(String filename) {
File file = new File(filename);
InputStream is = null;
Reader reader = null;
BufferedReader bufferedReader = null;
try {
is = new FileInputStream(file);
reader = new InputStreamReader(is);
bufferedReader = new BufferedReader(reader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
} } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != bufferedReader)
bufferedReader.close();
if (null != reader)
reader.close();
if (null != is)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} /**
* 按字节读取文件
*
* @param filename
*/
public static void ReadFileByBytes(String filename) {
File file = new File(filename);
InputStream is = null;
try {
is = new FileInputStream(file);
int index = 0;
while (-1 != (index = is.read())) {
System.out.write(index);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (null != is)
is.close(); } catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("-----------------------------------");
try {
is = new FileInputStream(file);
byte[] tempbyte = new byte[1000];
int index = 0;
while (-1 != (index = is.read(tempbyte))) {
System.out.write(tempbyte, 0, index);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (null != is)
is.close(); } catch (IOException e) {
e.printStackTrace();
}
}
} /**
* 按字符读取文件
*
* @param filename
*/
public static void ReadFileByChar(String filename) {
File file = new File(filename);
InputStream is = null;
Reader isr = null;
try {
is = new FileInputStream(file);
isr = new InputStreamReader(is);
int index = 0;
while (-1 != (index = isr.read())) {
System.out.print((char) index);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != is)
is.close();
if (null != isr)
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} /**
* 通过OutputStreamWriter写文件
*
* @param filename
*/
public static void Write2FileByOutputStream(String filename) {
File file = new File(filename);
FileOutputStream fos = null;
// BufferedOutputStream bos = null;
OutputStreamWriter osw = null;
try {
if (!file.exists()) {
file.createNewFile();
}
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos);
osw.write("Write2FileByOutputStream");
// bos = new BufferedOutputStream(fos);
// bos.write("Write2FileByOutputStream".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != osw) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != fos) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} /**
* 通过BufferedWriter写文件
*
* @param filename
*/
public static void Write2FileByBuffered(String filename) {
File file = new File(filename);
FileOutputStream fos = null;
OutputStreamWriter osw = null;
BufferedWriter bw = null;
try {
if (!file.exists()) {
file.createNewFile();
}
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos);
bw = new BufferedWriter(osw);
bw.write("Write2FileByBuffered");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != bw) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != osw) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != fos) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} /**
* 通过FileWriter写文件
*
* @param filename
*/
public static void Write2FileByFileWriter(String filename) {
File file = new File(filename);
FileWriter fw = null;
try {
if (!file.exists()) {
file.createNewFile();
}
fw = new FileWriter(file);
fw.write("Write2FileByFileWriter");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != fw) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} public static void main(String[] args) {
String filename = "D:/testfile.txt";
// ReadFileByLine(filename);
// ReadFileByBytes(filename);
// ReadFileByChar(filename);
String writeFile = "javawrite2file.txt";
// Write2FileByOutputStream(writeFile);
// Write2FileByBuffered(writeFile);
Write2FileByFileWriter(writeFile);
}
}
Java读写文件的大体情况应该就上面的几种方式,然而从效率的角度来讲。InputStream、OutputStream的效率比BufferedInputStream、BufferedOutputStream的效率要差,至于Reader、Writer没做进一步的比较。网上看到一些资料说Java.nio的效率最高,没有进一步做比较,不得而知了,等有时间再做进一步测试吧。
Java读写文件的几种方式的更多相关文章
- 【文件下载】Java下载文件的几种方式
[文件下载]Java下载文件的几种方式 摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...
- Python读写文件的几种方式
一.pandas pandas模块是数据分析的大杀器,它使得对于文件相关的操作变得简单. 看一下它的简单使用 import pandas as pd # 读取 df = pd.read_csv('al ...
- android 随手记 读写文件的几种方式
java中多种方式读文件 一.多种方式读文件内容. 1.按字节读取文件内容 2.按字符读取文件内容 3.按行读取文件内容 4.随机读取文件内容 */ import java.io.BufferedRe ...
- Java读取文件的几种方式
package com.mesopotamia.test; import java.io.BufferedReader; import java.io.ByteArrayInputStream; im ...
- delphi之读写文件的三种方式
一.Tstrings.Tstringlist procedure TForm1.Button2Click(Sender: TObject); var strlist: TStringList; pat ...
- golang读写文件的几种方式
golang中处理文件有很多种方式,下面我们来看看. (1)使用os模块 先来看看如何查看文件属性 package main import ( "fmt" "os&quo ...
- java追加文件的几种方式
import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.FileWriter; import ja ...
- java复制文件的4种方式
尽管Java提供了一个可以处理文件的IO操作类.但是没有一个复制文件的方法.复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候.然而有几种方法可以进行Java文件复制操作,下面列举出4中最 ...
- [JAVA]java复制文件的4种方式
尽管Java提供了一个可以处理文件的IO操作类. 但是没有一个复制文件的方法. 复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候. 然而有几种方法可以进行Java文件复制操作,下面列举出 ...
随机推荐
- 【BZOJ 3223】文艺平衡树 模板题
就是打个翻转标记,下推标记时记得交换左右孩子指针,查询kth和中序遍历输出时也记得要下推标记同时交换指针,二者不可缺!←这是易错点 仿陈竞潇学长模板的代码: #include<cctype> ...
- Code Review Engine Learning
相关学习资料 https://www.owasp.org/index.php/Code_review https://www.owasp.org/images/8/8e/OWASP_Code_Revi ...
- Longest Increasing Common Subsequence (LICS)
最长上升公共子序列(Longest Increasing Common Subsequence,LICS)也是经典DP问题,是LCS与LIS的混合. Problem 求数列 a[1..n], b[1. ...
- android选择时间攻略
安卓开发过程中难免会碰到需要选择日期时间的情况,由于大部分android初级教程都没教怎么选择时间,初学者碰到这种难免会有些不知所措,难道要让用户自己输入日期时间?先不说用户体验不好,处理用户输入各式 ...
- MyEclipse------executeBatch()使用方法
executeBatch()方法用于成批地执行SQL语句,但不能执行返回值是ResultSet结果集的SQL语句,而是直接执行stmt.executeBatch(); 辅助方法: addBatch() ...
- 线性判别分析(LDA)准则:FIsher准则、感知机准则、最小二乘(最小均方误差)准则
准则 采用一种分类形式后,就要采用准则来衡量分类的效果,最好的结果一般出现在准则函数的极值点上,因此将分类器的设计问题转化为求准则函数极值问题,即求准则函数的参数,如线性分类器中的权值向量. 分类器设 ...
- linux经典命令学习
本文介绍Linux系统的若干经典命令的常用方法. (一)grep 主要用于搜索文件内容,查看是否跟要求的pattern相匹配. 1.grep -l 'boss' * 显示所有包含boss ...
- linux下防火墙开启某个端口号及防火墙常用命令使用
linux防火墙常用命令 1.永久性生效,重启后不会复原 开启:chkconfigiptables on 关闭:chkconfigiptables off 2.即时生效,重启后复原 重启防火墙 方式一 ...
- C#面向对象面试题集锦
1.简述C#中的虚方法 答:注意:当使用virtual关键字修饰符后,不允许再同时使用abstract,static,或override关键字进行修饰 使用virtual关键字修饰的方法就是虚方法,虚 ...
- 汉诺塔问题II(模拟)
汉诺塔问题II Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1556 Solved: 720 Description 汉诺塔(又称河内塔)问题是源于 ...