请教:如何将String里边的内容按原格式写入文件

public static String read(String file) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
String s;
while ((s = in.readLine()) != null) {
sb.append(s+"\n");
}
sb.deleteCharAt(sb.length()-1);
in.close();

    } catch (IOException ex) {
        Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
    }
    return sb.toString();

}

将上边代码读取到的内容再用BufferedWriter写到文件中时 换行符都没了
如何才能保持String里的格式写入文件啊

注:不是做copy用

1、在windows中就用\r\n,在linux中用\n.
2、建议LZ不要使用硬编码,而用String LINE_SEPARATOR=System.getProperty("line.separator"),这个LINE_SEPARATOR就是一个回车换行符,这样跨系统也不用怕了.

sb.deleteCharAt(sb.length()-1);
这一行去掉吧。

用BufferedWriter的println,读一行就写一行

没读懂LZ的意思,能举个例子描述下吗

[quote]while ((s = in.readLine()) != null) { [/quote]
你这不就是读了一行吗???
直接把读到的这一行(s)写到文件里不就行了,不要全部拼好啊

[code="java"]
public static void read(String file) {
// StringBuilder sb = new StringBuilder();
PrintWriter out = null;
BufferedReader in = null;
try {
out = new PrintWriter(new OutputStreamWriter(new FileOutputStream("aa")));
in = new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
String s;
while ((s = in.readLine()) != null) {
out.println(s);
// sb.append(s+"\n");
}
// sb.deleteCharAt(sb.length()-1);
in.close();

    } catch (IOException ex) {

// Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (in != null) in.close();
} catch (IOException e) {
e.printStackTrace();
}
if (out != null) out.close();
}
// return sb.toString();
}
[/code]

改成差不多这个样子,不就可以了?

[code="java"]
public static String read(String file) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
String s;
while ((s = in.readLine()) != null) {
sb.append(s+"\r\n");
}
sb.deleteCharAt(sb.length()-1);
in.close();

    } catch (IOException ex) {
        Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
    }
    return sb.toString();

}
[/code]