Java 对文件的操作
public class ReadFile {
/**
* 按行读取文件操作
* @throws IOException
*/
public void readFile(String fileName) throws IOException{
//(1)File 类
File file = new File(fileName);
//
BufferedReader reader = null;
try {
//(2) 将文件放入到BufferedReader中
reader = new BufferedReader(new FileReader(file));
String temp = null;
int line = 0;
while( (temp = reader.readLine()) != null){
System.out.println(temp + (++line));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
reader.close();
}
}
/**
* 文件的写入操作
*/
public void writeFile(String fileName, String str) throws IOException{
File file = new File(fileName);
//true实现对文件的追加操作
FileWriter ws = new FileWriter(file,true);
ws.write(str);
ws.close();
}
/**
* 对于一个大文本文件,我们仅仅读取最后的N行
* @throws IOException
*/
public String[] getLastNFromFile(String fileName) throws IOException{
String []temp = new String[5];
File f = new File(fileName);
BufferedReader reader = new BufferedReader(new FileReader(f));
String temp1 = null;
int line = 0;
while((temp1 = reader.readLine()) != null){
temp[line++]= temp1;
if(line >= 5 ){
line = 0;
}
}
return temp;
}
/**
* 通过索引进行操作
* @throws IOException
*/
public String[] getLastNFromFileByIndex(String fileName) throws IOException{
String []temp = new String[5];
File f = new File(fileName);
BufferedReader reader = new BufferedReader(new FileReader(f));
String temp1 = null;
int line = 0;
while((temp1 = reader.readLine()) != null){
line++;
}
return temp;
}
}
对2000000行的文件进行操作,读取最后的5行,并没有发现直接通过行索引和通过一个数组进行栈式进入有什么差别!
Java 对文件的操作的更多相关文章
- loadrunner 脚本开发-调用java jar文件远程操作Oracle数据库测试
调用java jar文件远程操作Oracle数据库测试 by:授客 QQ:1033553122 测试环境 数据库:linux 下Oracle_11g_R2 Loadrunner:11 备注:想学ora ...
- Java的文件读写操作
file(内存)----输入流---->[程序]----输出流---->file(内存) 当我们读写文本文件的时候,采用Reader是非常方便的,比如FileReader,InputStr ...
- Java的文件读写操作 <转>
目录: file内存----输入流----程序----输出流----file内存 java中多种方式读文件 判断文件是否存在不存在创建文件 判断文件夹是否存在不存在创建文件夹 java 写文件的三种方 ...
- [转]Java的文件读写操作
file(内存)----输入流---->[程序]----输出流---->file(内存) 当我们读写文本文件的时候,采用Reader是非常方便的,比如FileReader,InputStr ...
- java大文件读写操作,java nio 之MappedByteBuffer,高效文件/内存映射
java处理大文件,一般用BufferedReader,BufferedInputStream这类带缓冲的Io类,不过如果文件超大的话,更快的方式是采用MappedByteBuffer. Mapped ...
- java 实现文件读写操作
import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; /* JAVA IO 读写操作 20 ...
- java写文件读写操作(IO流,字节流)
package copyfile; import java.io.*; public class copy { public static void main(String[] args) throw ...
- java 的文件读取操作
/** * @param filePath 文件的全路径 * 返回我们读取到的文件内容 * **/ public static String readFile(String filePath) { F ...
- java写文件读写操作(IO流,字符流)
package copyfile; import java.io.*; public class copy { public static void main(String[] args) throw ...
随机推荐
- MYSQL基础--学习笔记
最近一段时间,系统的学习了下mysql相关知识,当然都是比较基础的,现在贴出来,以供参考备忘--帅帅的小猪猪 创建用户:CREATE USER 'sampadm'@'localhost' IDENTI ...
- jsp_设置文件编码
jsp有两种方法可以设置文件编码: (1)<%@page language="java" contentType="text/html;charset=utf-8& ...
- iOS多线程编程之NSThread的使用(转)
本文由http://blog.csdn.net/totogo2010/原创 1.简介: 1.1 iOS有三种多线程编程的技术,分别是: 1..NSThread 2.Cocoa NSOperation ...
- 6.9 Android 优缺点
Android N主要在运行时和图形处理上做了更新. 运行时间上,Android N对编译器进行了优化,软件的运行时间提升了3-6倍.引入了一个全新的JIT编译器,使得App安装速度快了75%,编译代 ...
- halcon的算子列表
Chapter 1 :Classification 1.1 Gaussian-Mixture-Models 1.add_sample_class_gmm 功能:把一个训练样本添加到一个高斯混合模型的训 ...
- poj 1936 All in All
All in All Time Limit: 1000 MS Memory Limit: 30000 KB 64-bit integer IO format: %I64d , %I64u Java ...
- haskell中的do
在haskell中,有一个do的语句专门用来做一些不那么“干净”的事情,比如读写都需要用do来开头 一开始以为do的作用是做monad,后来发现是错误的,其实do做的事情是包裹一个顺序操作 比如在如下 ...
- ASP.NET Core 1.0 中使用 Swagger 生成文档
github:https://github.com/domaindrivendev/Ahoy 之前文章有介绍在ASP.NET WebAPI 中使用Swagger生成文档,ASP.NET Core 1. ...
- Arcgis for Javascript 在VS2012中的智能提示
官方地址: https://developers.arcgis.com/en/javascript/jsapi/api_codeassist.html 安装步骤 Visual Studio 2010 ...
- 设计模式之美:Adapter(适配器)
索引 别名 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):简单直接的对象适配器. 实现方式(二):实现双向类适配器. 别名 包装器(Wrapper) 意图 将一个类的接口转换成客户 ...