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. ADO是什么?

    ADO是一个组件,ADO不适于MFC但是可以在MFC里面使用.(ADO在1996年冬被发布.) 由于ADO在MFC使用的比较频繁,所以一些前辈将ADO的三个智能指针封装了. 之后就可以在MFC 更方便 ...

  2. Linux系统基础命令总结

    一.帮助命令(1)helpxxxx --helphelp xxxx(2)manman xxxx二.ls查看命令:列表显示目录内的文件及目录ls 参数-l 以列表的形式显示-d 显示目录本身属性-a显示 ...

  3. 如何获知PHP程序占用多少内存(复制)

    想要知道编写的 PHP 脚本需要占用多少内存么?很简单,直接使用 PHP 查看当前分配给 PHP 脚本的内存的函数 memory_get_usage() 就可以了 下面是使用示例: 复制代码 代码如下 ...

  4. 转:js获取json中key所对应的value值

    <script type="text/javascript"> getJson('age'); function getJson(key){ var jsonObj={ ...

  5. .net操作xml文件(新增.修改,删除,读取) 转

    今天有个需求需要操作xml节点.突然见遗忘了许多.上网看了些资料.才整出来.脑袋真不够用.在这里把我找到的资料共享一下.方便以后使用.本文属于网摘/ 1 一.简单介绍2 using System.Xm ...

  6. javascript面向对象笔记(一)

    ECMAscript对象(以下简称对象): ECMA-262把对象定义为:无序属性的集合,其属性可以包含基本值.对象或者函数. 对象的每个属性或方法都有一个名字,而每个名字都映射到一个值.值可以是数据 ...

  7. PKU 1035 Spell checker(Vector+String应用)

    题目大意:原题链接 1输入一个字符串,如果字典中存在该字符串,直接输出,否则; 2.删除,替换,或插入一个字母.如果使得输入字符串==字典中已经有的单词,输出该单词,否则. 3.直接输入下一个字符串, ...

  8. 一道仅有7人通过的超5星微软比赛题目-------解题思路&优秀代码分享,邀你来“找茬儿”

    6月23日英雄会平台发布了一道难度为超5星的微软比赛题目,截止活动结束共有300多名编程爱好者参与线上答题,而最终通过者仅有7人,通过率仅为2%.为什么成绩如此出人意料?是因为题目的英文描述难以理解? ...

  9. The type javax.http.HttpServletRequest cannot be resolved.It is indirectly 解决办法

    原因:项目中缺少servlet-api.jar文件. 解决办法:将E:\tomcat\apache-tomcat-6.0.24\lib下的servlet-api.jar拷贝到项目中,然后编译即可.(根 ...

  10. c++对txt文件的读取与写入

    转自:http://blog.csdn.net/lh3325251325/article/details/4761575 #include <iostream> #include < ...