java的流不常用,每次学习完都懂,过了一段时间就全忘了。。。
记录下一点实用的东西。。。
需求: 截取文件的前250kb内容
public static void main(String[] args) {
read("data");
}
public static void read(String inputFileName) {
String root = "/Users/xxx/Desktop/data/";
inputFileName = root + inputFileName ;
String outputFileName = inputFileName + 250 + "kb.html";
inputFileName = inputFileName + ".html";
File input = null;
File out = null;
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(inputFileName));
out = new File(outputFileName);
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(out));
String line = null;
while((line = bufferedReader.readLine())!=null){
long len = out.length();
long max = 250 * 1024;
if (len < max) {
bufferedWriter.write(line);
bufferedWriter.newLine();
bufferedWriter.flush();
}else {
break;
}
}
bufferedWriter.close();
bufferedReader.close();
}catch (Exception e){
System.out.println(e.getMessage());
}
}