Java读文件

package 天才白痴梦;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader; public class JavaIO { /**
* 采用的是操作系统底层默认的编码方式,GBK等,非UTF8
* */ /**
* 以字节为单位读取文件内容,常用于读取二进制文件,比如图片、影像、声音等文件
* */
public static void readFileByBytes(String filename) {
File file=new File(filename);
FileInputStream in=null;
try {
System.out.println("以字节为单位读取文件,一次读一个字节: ");
in=new FileInputStream(file);
int temp=0;
while ((temp=in.read()) != -1) {
System.out.println(temp);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return ;
}
try {
System.out.println("以字节为单位读取文件,一次读多个字节: ");
byte[] temp=new byte[100];
int byteread=0;
in=new FileInputStream(file);
JavaIO.showAvailableBytes(in);
while ((byteread=in.read(temp)) != -1) {
System.out.write(temp,0,byteread);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) { }
}
}
}
/**
* 以字符为单位读取文件,常用于读文本,数字等类型的文件
* */
public static void readFileByChar(String filename) {
File file=new File(filename);
Reader reader=null;
try {
System.out.println("以字符为单位读取文件内容,一次一个字节:");
//InputStreamReader类:是字节向字符转换的桥梁
reader=new InputStreamReader(new FileInputStream(file));
int temp;
while ((temp=reader.read()) != -1) {
if (((char)temp) != '\r') {
System.out.println((char)temp);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("以字符为单位读取文件内容,一次读多个字节: ");
char[] temp=new char[30];
int charread=0;
reader=new InputStreamReader(new FileInputStream(filename));
while ((charread=reader.read(temp)) != -1) {
if ((charread == temp.length) && (temp[temp.length-1]!='\r')) {
System.out.println(temp);
} else {
for (int i=0; i<charread; i++) {
if (temp[i] == '\r') {
break;
} else {
System.out.println(temp[i]);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) { }
}
}
}
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
* */
public static void readFileByLine(String filename) {
File file=new File(filename);
BufferedReader reader=null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行: ");
reader=new BufferedReader(new FileReader(file));
String temp=null;
int line=1;
while ((temp=reader.readLine()) != null) {
System.out.println("line " + line + ": " + temp);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) { }
}
}
}
/**
* 随机读取文件内容
* */
public static void readFileByRandomAccess(String filename) {
RandomAccessFile randomfile=null;
try {
System.out.println("随机读取一段文件内容");
randomfile=new RandomAccessFile(filename,"r");
long fileLength=randomfile.length();
int beginIndex=(fileLength > 4 ? 4 : 0);
randomfile.seek(beginIndex);
byte[] bytes=new byte[10];
int byteread=0;
while ((byteread=randomfile.read(bytes)) != -1) {
System.out.write(bytes,0,byteread);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (randomfile != null) {
try {
randomfile.close();
} catch (IOException e) { }
}
}
}
private static void showAvailableBytes(InputStream in) {
try {
System.out.println("当前字节输入流中的字节数为:" + in.available());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String filename="E:\\BaiYiShaoNian.txt";
JavaIO.readFileByBytes(filename);
JavaIO.readFileByChar(filename);
JavaIO.readFileByLine(filename);
JavaIO.readFileByRandomAccess(filename);
}
}

Java写文件

package 天才白痴梦;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter; public class JavaIO2 { public static void main(String[] args) throws IOException {
String Path="E:\\天才白痴梦\\JAVA";
File file=new File("E:\\天才白痴梦\\JAVA","BaiYiShaoNian.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Java写入文件的三种方法
* */
FileOutputStream fos=null;
BufferedWriter bw=null;
FileWriter fw=null;
int value=1000; try {
fos=new FileOutputStream(new File(Path+"fos.txt"));
long begin=System.currentTimeMillis();
for (int i=1; i<=value; i++) {
fos.write(5);
}
long end=System.currentTimeMillis();
System.out.println("TheCostTime of FileOutputStream is : " + (end-begin));
fos.close(); bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(Path+"br.txt")),"UTF8"));
begin=System.currentTimeMillis();
for (int i=1; i<=value; i++) {
bw.write(5);
bw.newLine();
}
bw.close();
end=System.currentTimeMillis();
System.out.println("TheCostTime of BufferedWriter is : " + (end-begin)); fw=new FileWriter(Path+"fw.txt");
begin=System.currentTimeMillis();
for (int i=1; i<=value; i++) {
fw.write(5);
}
fw.close();
end=System.currentTimeMillis();
System.out.println("TheCostTime of FileWriter is : " + (end-begin)); } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fos.close(); //FileOutputStream
bw.close(); //BufferedWriter
fw.close(); //FileWriter
} catch (Exception e) {
e.printStackTrace();
}
} }
}

Java读写文化总结的更多相关文章

  1. Java读写文本文件操作

    package com.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; ...

  2. java 读写word java 动态写入 模板文件

    import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import ja ...

  3. Java读写文件方法总结

    Java读写文件方法总结 Java的读写文件方法在工作中相信有很多的用处的,本人在之前包括现在都在使用Java的读写文件方法来处理数据方面的输入输出,确实很方便.奈何我的记性实在是叫人着急,很多时候既 ...

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

    自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行 ...

  5. java读写文件大全

     java读写文件大全 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(char[] ch,int o ...

  6. Java读写Windows共享文件夹 .

    版权声明:本文为博主原创文章,未经博主允许不得转载. 项目常常需要有访问共享文件夹的需求,例如共享文件夹存储照片.文件等.那么如何使用Java读写Windows共享文件夹呢? Java可以使用JCIF ...

  7. 【转】Java 读写Properties配置文件

    [转]Java 读写Properties配置文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形 ...

  8. java 读写excle

    2014-04-16 20:38:20 java读写excel 晚上打算研究如何c来编写

  9. Java 读写Properties配置文件

    Java 读写Properties配置文件 JAVA操作properties文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了M ...

随机推荐

  1. Linux系统——vim编辑器

    一.Linux系统中最常用的文本编辑器 vi:类Unix系统中默认的文本编辑器 vim:vi编辑器的增强版 作用:创建或修改文本文件:维护Linux系统中各种配置文件 二.三种切换模式命令模式输入模式 ...

  2. obj比较

    <script> var obj1={name:'one',age:30}; var obj2={name:'one',age:30}; //比较两个字符串对象的[值]是否相等 Objec ...

  3. python中的引用传递,可变对象,不可变对象,list注意点

    python中的引用传递 首先必须理解的是,python中一切的传递都是引用(地址),无论是赋值还是函数调用,不存在值传递. 可变对象和不可变对象 python变量保存的是对象的引用,这个引用指向堆内 ...

  4. C#数组的Map、Filter、Reduce操作

    在Javascript.Python等语言里,Map.Filter和Reduce是数组的常用方法,可以让你在实现一些数组操作时告别循环,具有很高的实用价值.它们三个的意义大家应该都清楚,有一个十分形象 ...

  5. C++文件路径的写法

    转自:http://blog.csdn.net/chengonghao/article/details/51057780 文件路径的表示可以分为绝对路径和相对路径: c++中\\是一种转义字符,他表示 ...

  6. spring-boot单元测试

    一.为什么要写单元测试 很多程序员有两件事情不愿意做: 写注释. 写单元测试. 但是在看代码时又会希望有清晰明了的注释,重构代码时能有一套随时可以跑起来的单元测试. 最近在迁移一个项目,从sqlser ...

  7. Python面试题之Python面向对象编程汇总

    面向对象的设计思想是从自然界中来的,因为在自然界中,类(Class)和实例(Instance)的概念是很自然的.Class是一种抽象概念,比如我们定义的Class——Student,是指学生这个概念, ...

  8. clientWidth offsetWidth scrollWidth

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  9. 20145303 实验二 Java面向对象程序设计

    实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验要求 1.没有Lin ...

  10. 20145324 Java实验一

    北京电子科技学院(BESTI) 实 验 报 告 课程:JAVA 班级:1453 姓名:王嘉澜 学号:20145324 成绩: 指导教师:娄嘉鹏 实验日期:2016.4.8 实验密级: 预习程度: 实验 ...