- 浏览: 170675 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (159)
- java常用 (28)
- Spring Security (0)
- tomcat (2)
- orcale (4)
- rmi (6)
- jvm (5)
- html (0)
- hibernate (0)
- javascript (5)
- log4j (5)
- struts (5)
- jdbc (0)
- jquery (0)
- json (0)
- jstl (0)
- jboss (0)
- JCaptcha (0)
- jpa (0)
- oscache (0)
- 项目管理 (8)
- ExtJs (10)
- 设计模式 (3)
- jsp (3)
- 错误归档 (1)
- maven (5)
- java多线程 (10)
- quartz (4)
- spring (9)
- memcache (3)
- java笔试面试 (3)
- xml (3)
- hadoop (5)
- velocity (2)
- jetty (3)
- http (1)
- linux (2)
- hbase (2)
- mysql (12)
- mybatis (8)
- web安全 (1)
- hive (2)
- 其他 (1)
最新评论
java 读写txt文件
读取:
public class ReadFromFile {
/**
* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*/
public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
System.out.println("以字节为单位读取文件内容,一次读一个字节:");
// 一次读一个字节
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
try {
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
// 一次读多个字节
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
// 读入多个字节到字节数组中,byteread为一次读入的字节数
while ((byteread = in.read(tempbytes)) != -1) {
System.out.write(tempbytes, 0, byteread);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}
}
/**
* 以字符为单位读取文件,常用于读文本,数字等类型的文件
*/
public static void readFileByChars(String fileName) {
File file = new File(fileName);
Reader reader = null;
try {
System.out.println("以字符为单位读取文件内容,一次读一个字节:");
// 一次读一个字符
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1) {
// 对于windows下,\r\n这两个字符在一起时,表示一个换行。
// 但如果这两个字符分开显示时,会换两次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("以字符为单位读取文件内容,一次读多个字节:");
// 一次读多个字符
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
// 读入多个字符到字符数组中,charread为一次读取字符数
while ((charread = reader.read(tempchars)) != -1) {
// 同样屏蔽掉\r不显示
if ((charread == tempchars.length)
&& (tempchars[tempchars.length - 1] != '\r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
/**
* 随机读取文件内容
*/
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;
// 将读文件的开始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
// 将一次读取的字节数赋给byteread
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 e1) {
}
}
}
}
/**
* 显示输入流中还剩的字节数
*/
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 = "C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
写入:
package com.sts.common;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
public class FileWriteCommon {
public static void main(String[] args) throws Exception {
Date dt = new Date();// 当前时间
Long time = dt.getTime();// 这就是距离1970年1月1日0点0分0秒的毫秒数
String times = String.valueOf(time);// 毫秒数类型转换成String
// 在d盘上创建一个名为testfile的文本文件
File f = new File("D:" + File.separator + times + ".txt");
// 用FileOutputSteam包装文件,并设置文件可追加
OutputStream out = new FileOutputStream(f, true);
// 字符数组
String str = "我是一个程序员";
out.write(str.getBytes()); // 向文件中写入数据
out.write('\r'); // \r\n表示换行
out.write('\n');
out.close(); // 关闭输出流
System.out.println("写入成功!");
}
}
发表评论
-
Java性能优化技巧
2013-03-30 11:19 712Java性能优化技巧 在JAVA程序中,性能问题的大部分原 ... -
java内部类的作用
2013-03-30 11:06 723java内部类的作用一、 ... -
StringTokenizer 用法
2013-03-25 09:29 1252StringTokenizer是一个用来分隔String的应 ... -
Java日志框架
2013-03-17 17:17 883Java日志框架 Log4j Ap ... -
java nio Buffer 中 compact的作用
2013-03-12 17:29 1668java nio Buffer 中 compact的作用 ... -
java annotation 入门
2013-03-11 11:23 731一. 最常见的annotation@Override:用在方法 ... -
Java中assert关键字的实现
2013-03-08 17:12 1036Java中assert关键字的实现 J2SE 1.4 ... -
java正则表达式
2013-03-07 08:42 650众所周知,在程序开发中,难免会遇到需要匹配、查找、替换、判 ... -
java对cookie的操作
2013-03-10 10:31 786java对cookie的操作 java对cookie的操 ... -
eclipse中的.project 和 .classpath文件的具体作用
2013-02-27 16:38 667eclipse中的.project 和 .cla ... -
Java中Split函数的用法技巧
2013-02-25 13:48 1562Java中Split函数的用法 ... -
如何把安全证书导入到java中的cacerts证书库
2013-02-21 11:21 1921如何把安全证书导入到java中的cacerts证书库 在项 ... -
java Comparable Comparator 接口
2013-02-19 15:58 7521.什么是Comparable接口 此接口强行对实现它的每 ... -
java环境变量配置
2012-08-28 21:51 687java环境变量配置 windows xp下配置JDK环 ... -
Java应用程序添加退出事件响应
2012-08-22 20:37 1150一个完整的Java应用程序,通常至少要有一个应用程序的结束点。 ... -
java 读取 doc poi读取word中的表.txt
2012-08-22 20:34 1706import java.io.File; import j ... -
java压缩和解压缩例子
2012-08-21 21:26 1044java压缩和解压缩例子 压缩时,在压缩文件夹中新建目录,en ... -
Java反射,根据类名创建带参数的实例
2012-08-21 21:26 2184public Object dynCreateClassIns ... -
java异常时如何执行
2012-08-21 21:26 644代码如下: public class Test{ publi ... -
Java transient关键字
2012-08-21 21:27 657Java transient关键字 Volatile修饰的成员 ...
相关推荐
java读取TXT文件入库,java读取TXT文件入库,java读取TXT文件入库,
java读取txt文件,可以以文件路径构造这个流,:FileInputStream fin = new FileInputStream("d:/test.txt"); 然后使用这个流直接读取到文件,再使用reader构造BufferedReader,按行读取一整行的文本,作为字符串返回...
使用java语言,通过调用FileReader类读取txt文件中的数据。资源是可直接实现的代码。
java 读取txt文本文件中的数据并保存到数据库中源代码,假设txt已有格式,并以","分隔。其中的sql包需要自己去微软官网下载。
Java\读取TXT文件.txtJava\读取TXT文件.txt
java读取txt文件代码,轻松掌握java,不成问题
java file,java读取txt文档,java操作txt文档,读取文档的内容
环境:eclipse3.7.2 jre1.6
public static void readTxtFile(String filePath){ try { String encoding="gb2312";
Java代码 用于进行txt读取 逐行读取后进行后续操作 入库
java 创建txt_JAVA读取TXT文件、新建TXT文件、写入TXT文件
用java读取txt文件 并且格式化输出读取到的信息 用java读取excel的xml格式文件 对信息进行分类 并且格式化输出有效内容
java 读取txt Demo java 读取txt Demo
用Java程序实现对.txt文件的读写。txt文件可作为配置文件,以key-Value形式出现。
JAVA实现创建TXT文件 并执行读写操作
java利用io技术创建文件夹、读取txt文件、写入txt文件(覆盖、不覆盖均有)