62,614
社区成员
发帖
与我相关
我的任务
分享
String result = "";
File file = new File("E:\\testtesttest\\test2.txt");
InputStream in = null;
byte[] tempByte = new byte[1024];
int byteread = 0;
try {
in = new FileInputStream(file);
while ((byteread = in.read(tempByte)) != -1 ) {
result += new String(tempByte, 0, byteread);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
result = result.replaceAll("\n", "");
try{
PrintWriter pw = new PrintWriter(new File("E:\\testtesttest\\test3.txt"));
pw.print(result);
pw.flush();
pw.close();
}catch(Exception e){
e.printStackTrace();
}
System.out.println("over");
String filePath = "E:\\testtesttest\\test2.txt";
BufferedReader s = null;
try {
String str = "";
s = new BufferedReader(new InputStreamReader(new FileInputStream(
new File(filePath)), "GB2312"));
PrintWriter pw = new PrintWriter(new File("E:\\testtesttest\\test3.txt"),"GB2312");
while ((str = s.readLine()) != null) {
pw.print(str+"\r");//不写入“\n”
pw.flush();
}
pw.close();
} catch (FileNotFoundException e) {
System.out.println("找不到指定文件");
} catch (IOException e) {
System.out.println("读取文件失败");
} finally {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("over");