简体   繁体   English

读/写文件的Java程序,如记事本++,即它不会改变现有的文本编码并将新文本附加到文件中

[英]Java program to read/write file like notepad++ that is it will not change existing text encoding and append new text to file

I am trying to append file by java program for reading file I am using scanner inputFile = new Scanner(new FileInputStream("myfile"), "UTF-8");我正在尝试通过 java 程序附加文件以读取文件我正在使用扫描仪inputFile = new Scanner(new FileInputStream("myfile"), "UTF-8");
after reading enttire file I am storing it on this.existingData=new StringBuilder(100000);阅读整个文件后,我将其存储在this.existingData=new StringBuilder(100000); string builder and close the file inputFile.close();字符串生成器并关闭文件inputFile.close(); . .
for writing I am using用于写作我正在使用
outFile = new PrintWriter(new OutputStreamWriter(new FileOutputStream("myfile"), StandardCharsets.UTF_8));
i will add program generated data in this existingData and write it我将在此existingData添加程序生成的数据并写入
outFile.print(existingData); outFile.flush();
and close the file并关闭文件
outFile.close();

So after that, if I try to commit the file in svn the patch file shows entire file changed instead of just newly added data.所以在那之后,如果我尝试在 svn 中提交文件,补丁文件会显示整个文件已更改,而不仅仅是新添加的数据。 the svn diff shows only newly added change. svn diff 仅显示新添加的更改。 if I append generated data using notepad++ then in patch file only added content is displayed.如果我使用 notepad++ 附加生成的数据,那么在补丁文件中只显示添加的内容。

I want to make my java program to read/write a file like notpad++ that is it will not change existing text encoding and append new text to file.我想让我的 java 程序读/写一个像 notpad++ 这样的文件,它不会改变现有的文本编码并将新文本附加到文件中。

The problem is: you always use UTF-8 for reading and writing.问题是:您总是使用 UTF-8 进行读写。 And the file you read may be written usng a very different encoding.您读取的文件可能使用非常不同的编码编写。

Try to determine the correct encoding using one of the libraries: juniversalchardet or ICU and write the text using the right encoding.尝试使用以下库之一确定正确的编码: juniversalchardetICU,并使用正确的编码编写文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM