简体   繁体   中英

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");
after reading enttire file I am storing it on this.existingData=new StringBuilder(100000); string builder and close the file 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
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. the svn diff shows only newly added change. if I append generated data using notepad++ then in patch file only added content is displayed.

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.

The problem is: you always use UTF-8 for reading and writing. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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