做个笔记,针对java 的file的操作
1.根据文件名及字符串,写文件
public
static
boolean
writeJiang(String content,String path,String name)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
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();
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
catch(Exception e)...{
e.printStackTrace();
}
System.out.println("写文件完毕");
return true;
}
2.读文件,返回字符串
public
static
String readTxt(String path)
...
{
String array = "";
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try...{
//读取文本文件
File file = new File(path);
FileInputStream rdf = new FileInputStream(file);
byte[] s = new byte[rdf.available()];
int b = rdf.available();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
while((b=rdf.read(s, 0, b))!=-1)...{
String content = new String(s,0,b);
array = array+content;
}
rdf.close();
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
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 = "";
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try...{
//读取文件
content = readTxt(path + "/" + name);
System.out.println(path + "/" + name);
System.out.println("未删除记录之前得是:" + content);
//删除某一行
String[] a = content.split(" ");
StringBuffer d = new StringBuffer();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
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;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
catch(Exception e)...{
e.printStackTrace();
}
return result;
}