Java 书写困难';使用FileOutputStream将转换为.txt文件
问题是,当我读取一个字符串,然后尝试将每个字符分别写入一个Java 书写困难';使用FileOutputStream将转换为.txt文件,java,file,fileoutputstream,Java,File,Fileoutputstream,问题是,当我读取一个字符串,然后尝试将每个字符分别写入一个.txt文件时,尽管System.out.println将显示正确的字符,但当我将它们写入.txt文件时,的将写入一些奇怪的字符。为了说明这一点,这里有一个例子:假设我们有这一行第二个主题的100页布局相同。,我们希望使用以下代码将其写入.txt文件: public static void write(String Swrite) throws IOException { if(!file.exists()){ file.
.txt
文件时,尽管System.out.println
将显示正确的字符,但当我将它们写入.txt
文件时,的将写入一些奇怪的字符。为了说明这一点,这里有一个例子:假设我们有这一行第二个主题的100页布局相同。
,我们希望使用以下代码将其写入.txt
文件:
public static void write(String Swrite) throws IOException {
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fop=new FileOutputStream(file,true);
if(Swrite!=null)
for(final String s : Swrite.split(" ")){
fop.write(s.toLowerCase().getBytes());
fop.write(System.getProperty("line.separator").getBytes());
}
fop.flush();
fop.close();
}
对于单词,subject's
:subject–来说,编写的文件看起来像这样™s
。我不知道为什么会发生这种情况。试试下面的方法。它使您不必处理字符编码
PrintWriter pw = null;
try {
pw = new PrintWriter(file);
if (Swrite!=null)
for (String s : Swrite.split(" ")) {
pw.println(s);
}
}
}
finally {
if (pw != null) {
pw.close();
}
}
像这样的怎么样:
// The file to read the input from and write the output to.
// Original content: Second subject’s layout of same 100 pages.
File file = new File("C:\\temp\\file.txt");
// The charset of the file, in our case UTF-8.
Charset utf8Charset = Charset.forName("UTF-8");
// Read all bytes from the file and create a string out of it (with the correct charset).
String inputString = new String(Files.readAllBytes(file.toPath()), utf8Charset);
// Create a list of all output lines
List<String> lines = new ArrayList<>();
// Add the original line and than an empty line for clarity sake.
lines.add(inputString);
lines.add("");
// Convert the input string to lowercase and iterate over it's char array.
// Than for each char create a string which is a new line.
for(char c : inputString.toLowerCase().toCharArray()){
lines.add(new String(new char[]{c}));
}
// Write all lines in the correct char encoding to the file
Files.write(file.toPath(), lines, utf8Charset);
//从中读取输入并将输出写入的文件。
//原始内容:第二个主题的布局相同的100页。
File File=新文件(“C:\\temp\\File.txt”);
//文件的字符集,在我们的例子中是UTF-8。
Charset utf8Charset=Charset.forName(“UTF-8”);
//从文件中读取所有字节并从中创建一个字符串(使用正确的字符集)。
String inputString=新字符串(Files.readAllBytes(file.toPath()),utf8Charset);
//创建所有输出行的列表
列表行=新的ArrayList();
//为清晰起见,添加原始行,而不是空行。
行。添加(inputString);
行。添加(“”);
//将输入字符串转换为小写,并遍历其字符数组。
//而不是为每个字符创建一个字符串,它是一个新行。
for(字符c:inputString.toLowerCase().toCharArray()){
add(新字符串(新字符[]{c}));
}
//以正确的字符编码将所有行写入文件
Files.write(file.toPath(),line,utf8Charset);
这一切都与上面评论的使用的字符集有关。字符编码。您可以在OutputStreamWriter中包装OutputStream,并在其中指定字符集。我不知道您正在使用什么来读取文件,但应该支持相同的字符编码。例如,写入UTF-8并在记事本++中打开它(如果使用Windows),将Charset
参数传递给方法getByte()
@Romski,但是如何写入UTF-8
?是的,当我在记事本中打开它时,它会显示类似的内容。当然,我使用了一个BufferedReader
来读取文件。是否有必要将阅读代码也放在问题中?@tony2009 10041如何做?@lonesome Java字符串以UTF-16编码。因此,正如其他人提到的,您需要为getBytes
方法指定所需的字符集。例如,s.toLowerCase().getBytes(“UTF-8”)
如果希望将其编码为UTF-8,则应该可以使用。但是,您的代码有两个问题。首先,它将重写到现有文件中,结果只包含最后一行。第二,它不会用小写写,它只是您所需要的所有代码的一部分。如果要删除该文件;您需要添加代码。如果要降低fstring的大小写,则需要添加代码。我强调了如何避免调用getBytes
和字符编码问题。我没有要求删除文件,而是要求append
文件,因此所有字符串都将包含在文件的最终版本中,而不仅仅是最后一行。但与fileoutputstream
不同,我看不到printwriter附加文件的任何参数。