自己总结的Java实现文件的读写操作
来源:互联网 发布:apache kudu 编辑:程序博客网 时间:2024/06/05 11:54
做个笔记,针对java 的file的操作
1.根据文件名及字符串,写文件
public static boolean writeJiang(String content,String path,String name)...{
try...{
File file = new File(path);
System.out.println("文件的路径是:" + file+"/"+name);
BufferedWriter fileout = new BufferedWriter(new FileWriter(file+"/"+name,true));
fileout.write(content);
fileout.write(" ");
fileout.flush();
fileout.close();
}
catch(Exception e)...{
e.printStackTrace();
}
System.out.println("写文件完毕");
return true;
}
try...{
File file = new File(path);
System.out.println("文件的路径是:" + file+"/"+name);
BufferedWriter fileout = new BufferedWriter(new FileWriter(file+"/"+name,true));
fileout.write(content);
fileout.write(" ");
fileout.flush();
fileout.close();
}
catch(Exception e)...{
e.printStackTrace();
}
System.out.println("写文件完毕");
return true;
}
2.读文件,返回字符串
public static String readTxt(String path)...{
String array = "";
try...{
//读取文本文件
File file = new File(path);
FileInputStream rdf = new FileInputStream(file);
byte[] s = new byte[rdf.available()];
int b = rdf.available();
while((b=rdf.read(s, 0, b))!=-1)...{
String content = new String(s,0,b);
array = array+content;
}
rdf.close();
}
catch(Exception e)...{
e.printStackTrace();
}
return array;
}
String array = "";
try...{
//读取文本文件
File file = new File(path);
FileInputStream rdf = new FileInputStream(file);
byte[] s = new byte[rdf.available()];
int b = rdf.available();
while((b=rdf.read(s, 0, b))!=-1)...{
String content = new String(s,0,b);
array = array+content;
}
rdf.close();
}
catch(Exception e)...{
e.printStackTrace();
}
return array;
}
3.删除文件中某一行.ID是行号,path+name=File
public static int deleteTxt(String path,String name,int id)...{
int result = 0;
String content = "";
try...{
//读取文件
content = readTxt(path + "/" + name);
System.out.println(path + "/" + name);
System.out.println("未删除记录之前得是:" + content);
//删除某一行
String[] a = content.split(" ");
StringBuffer d = new StringBuffer();
for (int j = 0; j < a.length; j++) ...{
if (j != id)
d.append(a[j]).append(" ");
}
System.out.println("删除记录之后的为:" + d.toString());
//将修改后的写入文件
writeNewTxt(d.toString(),path,name);
result = 1;
}
catch(Exception e)...{
e.printStackTrace();
}
return result;
}
int result = 0;
String content = "";
try...{
//读取文件
content = readTxt(path + "/" + name);
System.out.println(path + "/" + name);
System.out.println("未删除记录之前得是:" + content);
//删除某一行
String[] a = content.split(" ");
StringBuffer d = new StringBuffer();
for (int j = 0; j < a.length; j++) ...{
if (j != id)
d.append(a[j]).append(" ");
}
System.out.println("删除记录之后的为:" + d.toString());
//将修改后的写入文件
writeNewTxt(d.toString(),path,name);
result = 1;
}
catch(Exception e)...{
e.printStackTrace();
}
return result;
}
- 自己总结的Java实现文件的读写操作
- java 自己做的对XML文件的读写操作
- Java的文件读写操作
- Java的文件读写操作
- JAVA 文件的读写操作
- Java的文件读写操作
- java的文件读写操作
- Java的文件读写操作
- Java的文件读写操作
- Java的文件读写操作
- Java的文件读写操作
- Java的文件读写操作
- Java的文件读写操作
- Java的文件读写操作
- Java的文件读写操作
- Java的文件读写操作
- Java的文件读写操作
- Java的文件读写操作
- Spring入门以及Spring优点
- 抽象工厂模式
- 如何确定所运行的 SQL Server 2005 的版本
- Filter Functions 用例
- GridView 实现服务器端和客户端全选的两种方法
- 自己总结的Java实现文件的读写操作
- 计算机语言妙语录 软件就像性一样
- Web开发设计的五大准则
- Eclipse启动报“Required java version:1.4.1. Available:1.3.1_01.”
- Essential C# 2.0》读书笔记 第二章 数据类型
- Decode函数的语法
- Word2003的一些重要的细节
- 2007年12月19日 - 三人行(3user.com)新版v1.80发布
- DLL字符串参数做返回值的看法