转自:http://epasser.aydc.com.cn/article/adp/2/content14979.html
写入文件
try{
FileWriter fw=new FileWriter(SystemConfig.getRealPath()+"WEB-INF/url.txt");
fw.write("movie"+name);
fw.close();
}catch(IOException e){
e.printStackTrace();
}
读文件中内容
try{
FileReader fr = null;
fr = new FileReader(SystemConfig.getRealPath()+"WEB-INF/url.txt");
BufferedReader br=new BufferedReader(fr);
String Line = null;
String s = null;
Line = br.readLine();
while(Line!=null){
s+=Line;
Line=null;
}
br.close();
fr.close();
}
}catch(IOException e1){
e1.printStackTrace();
}
上传文件
try {
InputStream stream = getUpFile().getInputStream();//把文件读入
OutputStream bos = new FileOutputStream(filePath + "movie" +name);//建立一个上传文件的输出流
int bytesRead = 0;
byte[] buffer = new byte[1026];
while ( (bytesRead = stream.read(buffer, 0, 1026)) != -1) {
bos.write(buffer, 0, bytesRead);//将文件写入服务器
}
bos.close();
stream.close();
}catch(Exception e){
System.err.print(e);
}
FileWriter(File file, boolean append)
Constructs a FileWriter object given a File object. 构建一个给定File对象的FileWriter对象,append指明是否覆盖以前内容。