这是片段:
String s1=new String("127.0.0.1 www.google.com127.0.0.1 www.bing.com");
String s2=new String("127.0.0.1 www.google.com" + "\n" + "127.0.0.1 www.bing.com");
byte buffer1[]=s1.getBytes();
byte buffer2[]=s2.getBytes();
FileOutputStream fos=new FileOutputStream("f1.txt");
for(int i=0;i<buffer1.length;i++)
fos.write(buffer1[i]);
FileOutputStream fos2=new FileOutputStream("f2.txt");
for(int i=0;i<buffer2.length;i++)
fos2.write(buffer2[i]);
System.out.println("size of buffer1 = "+buffer1.length);
System.out.println("\nsize of buffer2 = "+buffer2.length);
String x=new String("s"+"\n"+"u");
System.out.println(x);
Run Code Online (Sandbox Code Playgroud)
我确实得到了2个文件f1.txt并f2.txt在我当前的目录中,但我希望a 127.0.0.1 www.bing.com在新行中,f2.txt但它出现在同一行(即文件相同f1.txt).
f2.txt当我在String构造函数中插入新的行字符时,为什么不进入新行?