admin 管理员组

文章数量: 1086019


2024年3月19日发(作者:他人即地狱免费观看高清)

java创建TXT文件并进行读、写、修改操作

文章分类:Java编程关键字: java创建txt文件并进行读、写、修改操作

import .*;

/** *//**

*

* 功能描述:创建TXT文件并进行读、写、修改操作

*

* @author KenZhang

* @version 1.0

* Creation date: 2007-12-18 - 下午06:48:45

*/

public class ReadWriteFile {

public static BufferedReader bufread;

//指定文件路径和名称

private static String path = "D:/";

private static File filename = new File(path);

private static String readStr ="";

/** *//**

* 创建文本文件.

* @throws IOException

*

*/

public static void creatTxtFile() throws IOException{

if (!()) {

NewFile();

n(filename + "已创建!");

}

}

/** *//**

* 读取文本文件.

*

*/

public static String readTxtFile(){

String read;

FileReader fileread;

try {

fileread = new FileReader(filename);

bufread = new BufferedReader(fileread);

try {

while ((read = ne()) != null) {

readStr = readStr + read+ "rn";

}

} catch (IOException e) {

// TODO Auto-generated catch block

tackTrace();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

tackTrace();

}

n("文件内容是:"+ "rn" + readStr);

return readStr;

}

/** *//**

* 写文件.

*

*/

public static void writeTxtFile(String newStr) throws IOException{

//先读取原有文件内容,然后进行写入操作

String filein = newStr + "rn" + readStr + "rn";

RandomAccessFile mm = null;

try {

mm = new RandomAccessFile(filename, "rw");

ytes(filein);

} catch (IOException e1) {

// TODO 自动生成 catch 块

tackTrace();

} finally {

if (mm != null) {

try {

();

} catch (IOException e2) {

// TODO 自动生成 catch 块

tackTrace();

}

}

}

}

/** *//**

* 将文件中指定内容的第一行替换为其它内容.

*

* @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();

// 保存该行前面的内容

for (int j = 1; (temp = ne()) != null

&& !(oldStr); j++) {

buf = (temp);

buf = (perty("tor"));

}

// 将内容插入

buf = (replaceStr);

// 保存该行后面的内容

while ((temp = ne()) != null) {

buf = (perty("tor"));

buf = (temp);

}

();

FileOutputStream fos = new FileOutputStream(file);

PrintWriter pw = new PrintWriter(fos);

(ng().toCharArray());

();

();

} catch (IOException e) {

tackTrace();

}

}

/** *//**

* main方法测试

* @param s

* @throws IOException

*/

public static void main(String[] s) throws IOException {

xtFile();

tFile();

xtFile("20080808:12:13");

// eTxtByStr("ken", "zhang");

}

}


本文标签: 文件 内容 创建 进行 地狱