自工作以后好久没有整理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读写文件的几种方式的更多相关文章

  1. 【文件下载】Java下载文件的几种方式

    [文件下载]Java下载文件的几种方式  摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...

  2. Python读写文件的几种方式

    一.pandas pandas模块是数据分析的大杀器,它使得对于文件相关的操作变得简单. 看一下它的简单使用 import pandas as pd # 读取 df = pd.read_csv('al ...

  3. android 随手记 读写文件的几种方式

    java中多种方式读文件 一.多种方式读文件内容. 1.按字节读取文件内容 2.按字符读取文件内容 3.按行读取文件内容 4.随机读取文件内容 */ import java.io.BufferedRe ...

  4. Java读取文件的几种方式

    package com.mesopotamia.test; import java.io.BufferedReader; import java.io.ByteArrayInputStream; im ...

  5. delphi之读写文件的三种方式

    一.Tstrings.Tstringlist procedure TForm1.Button2Click(Sender: TObject); var strlist: TStringList; pat ...

  6. golang读写文件的几种方式

    golang中处理文件有很多种方式,下面我们来看看. (1)使用os模块 先来看看如何查看文件属性 package main import ( "fmt" "os&quo ...

  7. java追加文件的几种方式

    import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.FileWriter; import ja ...

  8. java复制文件的4种方式

    尽管Java提供了一个可以处理文件的IO操作类.但是没有一个复制文件的方法.复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候.然而有几种方法可以进行Java文件复制操作,下面列举出4中最 ...

  9. [JAVA]java复制文件的4种方式

    尽管Java提供了一个可以处理文件的IO操作类. 但是没有一个复制文件的方法. 复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候. 然而有几种方法可以进行Java文件复制操作,下面列举出 ...

随机推荐

  1. 【uoj150】 NOIP2015—运输计划

    http://uoj.ac/problem/150 (题目链接) 题意 给出一棵树以及m个询问,可以将树上一条边的权值修改为0,求经过这样的修改之后最长的边最短是多少. Solution 老早就听说过 ...

  2. mysql存储过程的学习

    平时在工作中写过很多存储过程,但有时候对某些存储过程还是有些困惑的,所以发一篇文章记录下. 标准存储过程写法 create procedure`myQueryTask`( IN Task_No VAR ...

  3. Spring3.2.2之后不赞成使用queryForInt

    原来: public int getMatchCount(String username,String password){ String sql="select count(*) from ...

  4. <jsp:invoke fragment=""/>的理解和使用

    在传统 JSP 中,想要实现页面布局管理比较麻烦,为了解决在 JSP 中布局的问题,出现了很多开源软件,比如 Apache Tiles 和 SiteMesh 就是其中比较优秀的.但是使用开源软件实现布 ...

  5. 添加一个txt文件(例如在桌面),利用后台对文件写入内容

    string str = "今天天气好晴朗,处处好风光."; //需要将字符串转化成字节数组 byte[] buffer = Encoding.Default.GetBytes(s ...

  6. Linux中vi编辑器的用法

    实验一: vi编辑器的模式切换 1.       实验目标:熟练掌握vi编辑器的三种模式间切换及其特点 2.       实验操作步骤: 步骤一: 进入vi编辑器即命令模式 进入vi编辑器可以在命令终 ...

  7. Python初学笔记

    一.安装:直接通过软件管理程序,搜索Python,安装:安装过程中自定义路径,有个选项类似“add Python3.5 to Path”,勾选后便可以在cmd命令窗口,通过输入Python,启动Pyt ...

  8. JavaScript模块化学习基础

    http://www.ruanyifeng.com/blog/2012/10/javascript_module.html 一.原始写法 模块就是实现特定功能的一组方法. 不同函数简单放在一起就算一个 ...

  9. latex+bibtex+jabref(zz)

    很好的的latex使用心得: bibtex现学现卖 http://derecks.blog.sohu.com/118984444.html latex+bibtex+jabref http://blo ...

  10. MySql 创建只读账号

    GRANT Select ON *.* TO reader@192.168.1.123  IDENTIFIED BY "123456" GRANT  可以立刻生效 在mysql 5 ...