/* 获得socket输入流 ,允许每次读取一行 */
br = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
/* 获得文件输出流 ,允许每次写取一行 */
bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(savePath)));
/* 接收文件 */
System.out.println("开始接收文件!" + "\n");
String line = null;
while ((line = br.readLine()) != null) {
/*向本地写入文件*/
bw.write(line + "\r\n");
//bw.newLine();
}
bw.flush();以上代码不能向txt文件写入换行,几种方法都试过了,还是不行,我用下面代码获得系统的换行符时,返回的是null,本人系统是win7 64位。请大神们指教,是什么原因?String endLine = System.getProperty("line.separator", "\n"); // 获取换行符
String endLine2 = System.getProperty("line.separator"); // 获取换行符
System.out.println("换行符为" + endLine);
System.out.println("换行符为" + endLine2);
bw.write(line);
bw.write("\r\n");
String str="";
while ((line = br.readLine()) != null) {
str+=line+"\n";
}
bw.write(str);
bw.flush();
这样试试
bw.write(line + "\r\n");
//bw.newLine();
}
把这个 注释去掉 //bw.newLine();
* 生成.bat文件
*/
public static File proCmd(String ips) {
StringBuffer cmd = new StringBuffer();
Properties pro = null;
try {
pro = PropertiesUtils.getProps("scann.properties");
} catch (Exception e) {
e.printStackTrace();
}
String disk = (String) pro.get("DiskName");
String path = (String) pro.get("Path");
cmd.append(disk).append(":").append("\r\n").append("cd ").append(path).append("\r\n");
cmd.append("Xscan.exe -host "+ips+" -all");
System.out.println();
File file = new File(disk+":\\"+path+"\\"+ips+".bat");
FileWriter filew = null;
try {
filew = new FileWriter(file);
filew.write(cmd.toString());
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
filew.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
} 我以前写的方法,你自己改下
while((line=br.readLine())!=null){
pw.println(line);
}
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file=new File("D:\\aa.txt");
FileOutputStream outputStream=new FileOutputStream(file);
String s="aa\r\nbb";
byte[]b=s.getBytes();
outputStream.write(b);
outputStream.close(); }