java创建TXT文件并进行读、写、修改操作
import java.io.*;


/**
*
* 功能描述:创建TXT文件并进行读、写、修改操作
*
* @author <a href="mailto:zhanghhui@126.com">KenZhang</a>
* @version 1.0
* Creation date: 2007-12-18 - 下午06:48:45
*/
public class ReadWriteFile {
public static BufferedReader bufread;
//指定文件路径和名称
private static String path = "D:/suncity.txt";
private static File filename = new File(path);
private static String readStr ="";


/**
* 创建文本文件.
* @throws IOException
*
*/
public static void creatTxtFile() throws IOException{
if (!filename.exists()) {
filename.createNewFile();
System.err.println(filename + "已创建!");
}
}
/**
* 读取文本文件.
*
*/
public static String readTxtFile(){
String read;
FileReader fileread;
try {
fileread = new FileReader(filename);
bufread = new BufferedReader(fileread);
try {
while ((read = bufread.readLine()) != null) {
readStr = readStr + read+ "\r\n";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("文件内容是:"+ "\r\n" + readStr);
return readStr;
}
/**
* 写文件.
*
*/
public static void writeTxtFile(String newStr) throws IOException{
//先读取原有文件内容,然后进行写入操作
String filein = newStr + "\r\n" + readStr + "\r\n";
RandomAccessFile mm = null;
try {
mm = new RandomAccessFile(filename, "rw");
mm.writeBytes(filein);
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
} finally {
if (mm != null) {
try {
mm.close();
} catch (IOException e2) {
// TODO 自动生成 catch 块
e2.printStackTrace();
}
}
}
}
/**
* 将文件中指定内容的第一行替换为其它内容.
*
* @param oldStr
* 查找内容
* @param replaceStr
* 替换内容
*/
public static void replaceTxtByStr(String oldStr,String replaceStr) {
String temp = "";
try {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();

// 保存该行前面的内容
; (temp = br.readLine()) != null
&& !temp.equals(oldStr); j++) {
buf = buf.append(temp);
buf = buf.append(System.getProperty("line.separator"));
}

// 将内容插入
buf = buf.append(replaceStr);

// 保存该行后面的内容
while ((temp = br.readLine()) != null) {
buf = buf.append(System.getProperty("line.separator"));
buf = buf.append(temp);
}

br.close();
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* main方法测试
* @param s
* @throws IOException
*/
public static void main(String[] s) throws IOException {
ReadWriteFile.creatTxtFile();
ReadWriteFile.readTxtFile();
ReadWriteFile.writeTxtFile("20080808:12:13");
// ReadWriteFile.replaceTxtByStr("ken", "zhang");
}
}
修改单个文件字符串
String s="";
try{
FileInputStream fis=new FileInputStream("/home/liu_yin/Android/apktool1.5.2/javatest");//从d:\\1\\3.txt");读出内容到s
byte[]b=new byte[1024];
while(true){
int i=fis.read(b);
if(i==-1)break;
s=s+new String(b,0,i);
}
fis.close();
}catch(Exception e){
e.printStackTrace();
}
s=s.replaceAll("LogUtil", "//LogUtil");//,进行替换我开始以为是 换行呢,不会还百度了。要换行"\r\n"
try{
FileOutputStream fos=new FileOutputStream("/home/liu_yin/Android/apktool1.5.2/javatest",false);//true原有续写,false是追加。如果源文件不存在就新建了
fos.write(s.getBytes());
fos.close();
}catch(Exception e){
e.printStackTrace();
}
遍历文件夹下的所有文件
static void getFiles(String filePath){
File root = new File(filePath);
File[] files = root.listFiles();
for(File file:files){
if(file.isDirectory()){
/*
* 递归调用
*/
getFiles(file.getAbsolutePath());
filelist.add(file.getAbsolutePath());
System.out.println("显示"+filePath+"下所有子目录及其文件"+file.getAbsolutePath());
}else{
System.out.println("显示"+filePath+"下所有子目录"+file.getAbsolutePath());
}
}
}


java创建TXT文件并进行读、写、修改操作的更多相关文章
- JAVA 创建TXT文件,写入文件内容,读取文件内容
[java] view plain copy package com.abin.facade.ws.mail.function; import java.io.BufferedReader; i ...
- Java创建TXT文件并写入 内容
public static void main(String[] args) { String filePath = "E:/" + "1.txt"; Stri ...
- Java读写txt文件
1.Java读取txt文件 1.1.使用FileInputStream: public static String readFile(File file, String charset){ //设置默 ...
- java 读取TXT文件的方法
java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...
- java读取TXT文件的方法
java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...
- java读取txt文件内容
package read; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public ...
- Java读取txt文件信息并操作。
一.java读取txt文件内容 import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.Fi ...
- JAVA获取txt文件内容
JAVA 读取txt文件内容 通常,我们可以直接通过文件流来读取txt文件的内容,但有时可能会出现乱码!此时只要设置一下文件字符编码即可. public class txttest { /** * 读 ...
- Java读取txt文件、excel文件的方法
Java读取txt文件.excel文件的方法 1.读取txt文件 public static String getFileContent(String filePath,String charset) ...
随机推荐
- Git与github常用命令
Git项目与github建立联系 首先,需要在github上建立一个repository mkdir github-project cd github-project git init 此时githu ...
- rem自适应原理
rem自适应原理 rem是根据html的font-size大小来变化,正是基于这个出发,我们可以在每一个设备下根据设备的宽度设置对应的html字号,从而实现了自适应布局.更多介绍请看这篇文章:rem是 ...
- IMAP简单研究
IMAP的相关详细介绍: http://www.imapwiki.org/ClientImplementationhttp://tools.ietf.org/html/rfc3501 1.连接服务器 ...
- php 操作提示框
/** * 跳转 * @param type $msg * @param type $url */ protected function jump($msg, $url) { $html = < ...
- laravel加载js和css等资源
4里面是composer下载以后,publish,blade模板里面有html标签 不过在5以后,html和form标签去掉了,publish方式似乎也变化了,没看懂…… 直接贴demo吧 mac:n ...
- 301重定向方法大全及SEO中网址规范化,看着不错先收下
301重定向方法大全及SEO中网址规范化 现在大多数网站都存在一些内容相同但网址(URL)不一样的重复内容,这些重复的内容对于搜索引擎来说却可能被认为是复制网页,复制网页虽然不会被惩罚但因多个网址存在 ...
- java代码---------打印正三角形
总结:手打一遍 public class aaa{ public static void main(String []args){ for(int i=1;i<10;i++){ for(int ...
- java代码----------实现创建DataInputStream和DataOutputStream进行读写
总结: 主要是 捕获异常 package com.a.b; import java.io.*; public class testData { public static void main(Stri ...
- [Web]网址净化方法
本文来自:https://meta.appinn.com/t/topic/3130 原理很简单,所以不说了. 用法很简单,先把下面的代码保存为书签(复制到地址里面),在需要的页面里点击一下这个书签就好 ...
- 翻译内核uvcvideo.txt
翻译源:linux-3.4.20\Documentation\video4linux\uvcvideo.txt 译文: Linux USB视频类(UVC)驱动程序 ================== ...