1.读文件
<span style="color:#FF0000;">File file = new File("d:/test.txt"); </span>
<span style="color:#FF0000;"> InputStream in = null; </span>
try {
System.out.println("以字节为单位读取文件内容,一次读一个字节:");
// 一次读一个字节
<span style="color:#FF0000;"> in = new FileInputStream(file); </span>
int tempbyte;
<span style="color:#FF0000;">while ((tempbyte = in.read()) != -1) {
System.out.println(tempbyte);
}
in.close(); </span>
} catch (IOException e) {
e.printStackTrace();
return;
}
2.写文件
<span style="color:#FF0000;"> File file = new File("d:/test.txt");
String str = "亲爱的小南瓜!";
byte bt[] = new byte[1024];
bt = str.getBytes(); </span>
try {
<span style="color:#FF0000;"> FileOutputStream in = new FileOutputStream(file); </span>
try {
<span style="color:#FF0000;"> in.write(bt);
in.close();
</span> // boolean success=true;
// System.out.println("写入文件成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
3.判断文件是否存在,如果不存在创建新文件
<span style="color:#FF0000;"> File file=new File(path+filename); </span>
if(<span style="color:#FF0000;">!file.exists()</span>)
{
try {
<span style="color:#FF0000;"> file.createNewFile(); </span>
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
4.判断文件夹是否存在,如果不存在创建新文件夹
<span style="color:#FF0000;">File file =new File(path+filename); </span>
//如果文件夹不存在则创建
if (<span style="color:#FF0000;">!file .exists()</span>)
{
<span style="color:#FF0000;"> file .mkdir(); </span>
}