Java文件运用
2014-01-04 17:07
211 查看
1.使用java修改文件内容:
package fileopt;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* 修改文件
*/
public class FileModify {
/**
* 读取文件内容
*
* @param filePath
* @return
*/
public String read(String filePath) {
BufferedReader br = null;
String line = null;
StringBuffer buf = new StringBuffer();
try {
// 根据文件路径创建缓冲输入流
br = new BufferedReader(new FileReader(filePath));
// 循环读取文件的每一行, 对需要修改的行进行修改, 放入缓冲对象中
while ((line = br.readLine()) != null) {
// 此处根据实际需要修改某些行的内容
if (line.startsWith("a")) {
buf.append(line).append(" start with a");
}
else if (line.startsWith("b")) {
buf.append(line).append(" start with b");
}
// 如果不用修改, 则按原来的内容回写
else {
buf.append(line);
}
buf.append(System.getProperty("line.separator"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭流
if (br != null) {
try {
br.close();
} catch (IOException e) {
br = null;
}
}
}
return buf.toString();
}
/**
* 将内容回写到文件中
*
* @param filePath
* @param content
*/
public void write(String filePath, String content) {
BufferedWriter bw = null;
try {
// 根据文件路径创建缓冲输出流
bw = new BufferedWriter(new FileWriter(filePath));
// 将内容写入文件中
bw.write(content);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭流
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
bw = null;
}
}
}
}
public void fileAppender(String fileName,String content) throws IOException{
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = null;
// 一行一行的读
StringBuilder sb = new StringBuilder();
sb.append(content);
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\r\n");
}
reader.close();
//写回去
RandomAccessFile mm = new RandomAccessFile(fileName, "rw");
mm.writeBytes(sb.toString());
mm.close();
}
/**
* 主方法
*/
public static void main(String[] args) {
String filePath = FileModify.class.getResource("").getPath()+"test.properties"; // 文件路径
FileModify obj = new FileModify();
obj.write(filePath, obj.read(filePath)); // 读取修改文件
try {
obj.fileAppender(filePath, "set a=b \n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用java修改文件内容package fileopt;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* 修改文件
*/
public class FileModify {
/**
* 读取文件内容
*
* @param filePath
* @return
*/
public String read(String filePath) {
BufferedReader br = null;
String line = null;
StringBuffer buf = new StringBuffer();
try {
// 根据文件路径创建缓冲输入流
br = new BufferedReader(new FileReader(filePath));
// 循环读取文件的每一行, 对需要修改的行进行修改, 放入缓冲对象中
while ((line = br.readLine()) != null) {
// 此处根据实际需要修改某些行的内容
if (line.startsWith("a")) {
buf.append(line).append(" start with a");
}
else if (line.startsWith("b")) {
buf.append(line).append(" start with b");
}
// 如果不用修改, 则按原来的内容回写
else {
buf.append(line);
}
buf.append(System.getProperty("line.separator"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭流
if (br != null) {
try {
br.close();
} catch (IOException e) {
br = null;
}
}
}
return buf.toString();
}
/**
* 将内容回写到文件中
*
* @param filePath
* @param content
*/
public void write(String filePath, String content) {
BufferedWriter bw = null;
try {
// 根据文件路径创建缓冲输出流
bw = new BufferedWriter(new FileWriter(filePath));
// 将内容写入文件中
bw.write(content);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭流
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
bw = null;
}
}
}
}
public void fileAppender(String fileName,String content) throws IOException{
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = null;
// 一行一行的读
StringBuilder sb = new StringBuilder();
sb.append(content);
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\r\n");
}
reader.close();
//写回去
RandomAccessFile mm = new RandomAccessFile(fileName, "rw");
mm.writeBytes(sb.toString());
mm.close();
}
/**
* 主方法
*/
public static void main(String[] args) {
String filePath = FileModify.class.getResource("").getPath()+"test.properties"; // 文件路径
FileModify obj = new FileModify();
obj.write(filePath, obj.read(filePath)); // 读取修改文件
try {
obj.fileAppender(filePath, "set a=b \n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.使用Java修改文件名
package fileopt;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* 修改文件
*/
public class FileModify {
/**
* 读取文件内容
*
* @param filePath
* @return
*/
public String read(String filePath) {
BufferedReader br = null;
String line = null;
StringBuffer buf = new StringBuffer();
try {
// 根据文件路径创建缓冲输入流
br = new BufferedReader(new FileReader(filePath));
// 循环读取文件的每一行, 对需要修改的行进行修改, 放入缓冲对象中
while ((line = br.readLine()) != null) {
// 此处根据实际需要修改某些行的内容
if (line.startsWith("a")) {
buf.append(line).append(" start with a");
}
else if (line.startsWith("b")) {
buf.append(line).append(" start with b");
}
// 如果不用修改, 则按原来的内容回写
else {
buf.append(line);
}
buf.append(System.getProperty("line.separator"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭流
if (br != null) {
try {
br.close();
} catch (IOException e) {
br = null;
}
}
}
return buf.toString();
}
/**
* 将内容回写到文件中
*
* @param filePath
* @param content
*/
public void write(String filePath, String content) {
BufferedWriter bw = null;
try {
// 根据文件路径创建缓冲输出流
bw = new BufferedWriter(new FileWriter(filePath));
// 将内容写入文件中
bw.write(content);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭流
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
bw = null;
}
}
}
}
public void fileAppender(String fileName,String content) throws IOException{
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = null;
// 一行一行的读
StringBuilder sb = new StringBuilder();
sb.append(content);
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\r\n");
}
reader.close();
//写回去
RandomAccessFile mm = new RandomAccessFile(fileName, "rw");
mm.writeBytes(sb.toString());
mm.close();
}
/**
* 主方法
*/
public static void main(String[] args) {
String filePath = FileModify.class.getResource("").getPath()+"test.properties"; // 文件路径
FileModify obj = new FileModify();
obj.write(filePath, obj.read(filePath)); // 读取修改文件
try {
obj.fileAppender(filePath, "set a=b \n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用java修改文件内容package fileopt;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* 修改文件
*/
public class FileModify {
/**
* 读取文件内容
*
* @param filePath
* @return
*/
public String read(String filePath) {
BufferedReader br = null;
String line = null;
StringBuffer buf = new StringBuffer();
try {
// 根据文件路径创建缓冲输入流
br = new BufferedReader(new FileReader(filePath));
// 循环读取文件的每一行, 对需要修改的行进行修改, 放入缓冲对象中
while ((line = br.readLine()) != null) {
// 此处根据实际需要修改某些行的内容
if (line.startsWith("a")) {
buf.append(line).append(" start with a");
}
else if (line.startsWith("b")) {
buf.append(line).append(" start with b");
}
// 如果不用修改, 则按原来的内容回写
else {
buf.append(line);
}
buf.append(System.getProperty("line.separator"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭流
if (br != null) {
try {
br.close();
} catch (IOException e) {
br = null;
}
}
}
return buf.toString();
}
/**
* 将内容回写到文件中
*
* @param filePath
* @param content
*/
public void write(String filePath, String content) {
BufferedWriter bw = null;
try {
// 根据文件路径创建缓冲输出流
bw = new BufferedWriter(new FileWriter(filePath));
// 将内容写入文件中
bw.write(content);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭流
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
bw = null;
}
}
}
}
public void fileAppender(String fileName,String content) throws IOException{
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = null;
// 一行一行的读
StringBuilder sb = new StringBuilder();
sb.append(content);
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\r\n");
}
reader.close();
//写回去
RandomAccessFile mm = new RandomAccessFile(fileName, "rw");
mm.writeBytes(sb.toString());
mm.close();
}
/**
* 主方法
*/
public static void main(String[] args) {
String filePath = FileModify.class.getResource("").getPath()+"test.properties"; // 文件路径
FileModify obj = new FileModify();
obj.write(filePath, obj.read(filePath)); // 读取修改文件
try {
obj.fileAppender(filePath, "set a=b \n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.使用Java修改文件名
package script; import java.io.File; import java.io.IOException; public class Realname { public static void main(String[] args) throws IOException { File oldFile = new File("d:/PMS"); if(!oldFile.exists()) { oldFile.createNewFile(); } System.out.println("修改前文件名称是:"+oldFile.getName()); String rootPath = oldFile.getParent(); System.out.println("根路径是:"+rootPath); File newFile = new File(rootPath + File.separator + "PMSTmp"); System.out.println("修改后文件名称是:"+newFile.getName()); if (oldFile.renameTo(newFile)) { System.out.println("修改成功!"); } else { System.out.println("修改失败"); } } }
相关文章推荐
- fastjson(javabean命名)
- eclipse重命名慢解决方法
- java 字节流与字符流的区别
- Java中计算时间差
- Java中定时器
- Remove '@override' annotation解决办法
- ubuntu 12.04 系统中在eclipse中安装SVN 插件的时候的错误
- 上下文(context)ActionContext介绍(在Struts2中)
- JAVA计算字符串UTF-8字节数
- Java改变图片的大小
- Spring基于 Annotation 的简单介绍
- Spring中的PathMatchingResourcePatternResolver用于搜索资源
- 对JAVA集合进行遍历删除时务必要用迭代器 推荐
- java中的key接口解析
- Struts2 日志原理及配置方法(结合Log4j)
- Java的反射(一)
- 【jdk源码解析一】java.util.Observer与java.util.Observable
- Java中锁的应用之-ReadWriteLock
- java的System.getProperty()方法可以获取的值
- Java变量以及内存分配