When writing data from the console to a file, the encoding is lost. one

package com.mycompany.praktika; import static j2html.TagCreator.*; import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintStream; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class MainClass { static Scanner sc = null; static String name = null, region = null, first = null, second = null, name2 = null, three=null, firstname=null, secondname=null, threename=null, add=null,add2=null; static String ipn, scor, ipn2, scor2, prise, ipn3; public static void main(String[] args) throws IOException{ sc =new Scanner(new InputStreamReader(System.in)); System.out.println("---ОДЕРЖУВАЧ---"); first = ("---ОДЕРЖУВАЧ---"); System.out.println("Найменування: "); name = "Найменування: " + sc.next(); System.out.println("Регіон оплати: "); region = "Регіон оплати: " + sc.next(); System.out.println("ІПН: "); ipn = "ІПН: " + ifNotNumber(sc.next()); System.out.println("Номер рахунку: "); scor = "Номер рахунку: " + ifNotNumber(sc.next()); System.out.println("---БАНК ОТРИМУВАЧА---"); second = "---БАНК ОТРИМУВАЧА---"; System.out.println("Найменування: "); name2 = "Найменування: " + sc.next(); System.out.println("ІПН: "); ipn2 = "ІПН: " + ifNotNumber(sc.next()); System.out.println("Номер рахунку: "); scor2 = "Номер рахунку: " + ifNotNumber(sc.next()); System.out.println("Зняти з рахунку: "); prise = "Зняти з раххунку: " + ifNotNumber(sc.next()); System.out.println("---ДЕТАЛІ ПЛАТЕЖУ---"); three = "---ДЕТАЛІ ПЛАТЕЖУ---"; System.out.println("ІПН організіції/отримача: "); ipn3 = "ІПН організіції/отримача: " + ifNotNumber(sc.next()); System.out.println("Ім'я: "); firstname = "Ім'я: " + sc.next(); System.out.println("Фамілія: "); secondname = "Фамілія: " + sc.next(); System.out.println("По-батькові: "); threename = "По-батькові:" + sc.next(); System.out.println("Адреса платника: "); add = "Адреса платника: " + sc.next(); System.out.println("Телефон платника: "); add2 = "Телефон платника: " + sc.next(); String fina = first + "\n" + name +"\n" +region + "\n"+ ipn+ "\n"+ scor +"\n" + second + "\n" + name2 + "\n"+ipn2 + "\n" + scor2 + "\n" + prise + "\n" + three + "\n" + ipn3 + "\n" +firstname + "\n" + secondname+"\n" + threename + "\n" + add + "\n" + add2; createDocx(html( head( meta().withCharset("UTF-8"), title("Чек") ), body( style("display: table-row-group;"), h5(first).attr("style = \" display: table-row-group; \" ;"), h5(name).attr("style = \" display: table-row-group; \" ;"), h5(region).attr("style = \" display: table-row-group; \" ;"), h5(ipn).attr("style = \" display: table-row-group; \" ;"), h5(scor).attr("style = \" display: table-row-group; \" ;"), h5(second).attr("style = \" display: table-row-group; \" ;"), h5(name2).attr("style = \" display: table-row-group; \" ;"), h5(ipn2).attr("style = \" display: table-row-group; \" ;"), h5(scor2).attr("style = \" display: table-row-group; \" ;"), h5(prise).attr("style = \" display: table-row-group; \" ;"), h5(three).attr("style = \" display: table-row-group; \" ;"), h5(ipn3).attr("style = \" display: table-row-group; \" ;"), h5(firstname).attr("style = \" display: table-row-group; \" ;"), h5(secondname).attr("style = \" display: table-row-group; \" ;"), h5(threename).attr("style = \" display: table-row-group; \" ;"), h5(add).attr("style = \" display: table-row-group; \" ;"), h5(add2).attr("style = \" display: table-row-group; \" ;") ) ).render()); } static String ifNotNumber(String number){ try{ if (Double.parseDouble(number) >= 0) {} }catch(Exception e){ System.out.println("Введені не вірні данні: введіть знову"); ifNotNumber(sc.next()); return number; } return number; } public static void createDocx(String fina) throws IOException{ // HtmlCreator ht = new HtmlCreator(); //FileOutputStream fs = new FileOutputStream("Chek.html", StandardCharsets.UTF_8); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("Chek.html"), StandardCharsets.UTF_8); // OutputStreamWriter writer = new OutputStreamWriter(fs, StandardCharsets.UTF_8); writer.write(fina); writer.close(); } } 
  • exactly encoding error when reading from console? can she in the formation of html? go through debugging, see what is in the variables after reading from the console - Victor

2 answers 2

 public String getInput() { try { BufferedReader reader = new BufferedReader( new InputStreamReader (System.in)); String path = reader.readLine(); return path; } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } return null; } 

How to use: String input = getInput();

To write something to the file you can use this function.

 public void writeToFile(String pathToFile, String text) { try{ out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(pathToFile),"Cp1251")); out.write(text); out.newLine(); out.close(); } catch (IOException e){ System.out.println(e.getMessage()); } } 

The first argument is the path to the file to which you need to write something.

The second argument is the text itself to be written.

  • Why should a person write to a file if he has a problem reading data from the console? You just added a wrapped BufferedWriter wrapper to its code - Victor
  • Than FileWriter not happy? - And

It is important in what encoding you read the data. Those. find out the console encoding in your operating system. Perhaps it will need to switch to another. Or, when reading data, consider it (something like Scanner in = new Scanner(System.in, "UTF-8"); ).