在将带有换行的长String存到txt中保留原有的换行

例如:

String s  为:

123

wer

想在txt中以原格式存储,方法如下

关键点将字符流中的“/n”换成“/r/n”

		public static File Filein(String filename, String s) {

				FileWriter fw = null;
				String separator = File.separator;
				String directory = "E:" + separator + "workSpace" + separator
						+ "MyEclipse";
				File f = new File(directory, filename);
				try {
					if (!f.exists()) {
						f.getParentFile().mkdirs();
						f.createNewFile();
					}
					fw = new FileWriter(f);
					BufferedWriter out = new BufferedWriter(fw);
					out.write(s.replaceAll("\n", "\r\n"), 0, s.length());
					out.close();

				} catch (IOException e) {
					e.printStackTrace();
				}
				return f;
			}
发布了156 篇原创文章 · 获赞 134 · 访问量 50万+

猜你喜欢

转载自blog.csdn.net/skyxmstar/article/details/102622522