1

I think I need to use

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "UTF8"));

But when I change my code to BufferedWriter, then something is wrong with the line out.write(e);.

My code:

public void writeToFile(String fileName, List<ThongtinKhachThue> resultttkt){
        System.out.println("Begin....");
        try {
            PrintWriter out = new PrintWriter(new File(fileName));
            out.println("//thue.txt");
            for(ThongtinKhachThue e : resultttkt){
                    out.println(e);                 
            }
            out.println("Tổng khách thuê phòng: "+TongKhachThue());
            out.println("Tổng tiền thu: "+TongTienThuDuoc()+"$");
            out.flush();
            out.close();
            System.out.println("End...");
        } catch (FileNotFoundException e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
    }

what it shows in file:

//thue.txt
K1, A1002, 47.05$ 
K2, A0003, 67.0$ 
K3, A1001, 31.0$ 
K4, null, 0.0$ 
T?ng khách thuê ph?ng: 3
T?ng ti?n thu: 145.05$

what I want it to show is:

//thue.txt
K1, A1002, 47.05$ 
K2, A0003, 67.0$ 
K3, A1001, 31.0$ 
K4, Không thuê
Tổng khách thuê: 03
Tổng tiền thu: 145.05$

How can i write utf-8 to file and change "3" to "03"?

dcaswell
  • 3,137
  • 2
  • 26
  • 25
taki
  • 75
  • 1
  • 10

5 Answers5

3

To write 03 (leading zero) instead of 3 you can use

int myInteger = 3;
out.println("Some value: " + String.format("%02d", myInteger) );

See String.format(...) documentation for details on the syntax.

MrSnrub
  • 1,123
  • 1
  • 11
  • 19
0

Use,

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),"UTF-8"));

that way you can set the encoding.

john01dav
  • 95
  • 1
  • 4
0

You need to use the charset names mentioned at this link

It says the standard charsets as "UTF-8" and not "UTF8".

So change it to "UTF-8" and it will work.

Below for your reference :

  • US-ASCII
    Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set
  • ISO-8859-1
    ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
  • UTF-8
    Eight-bit UCS Transformation Format
  • UTF-16BE
    Sixteen-bit UCS Transformation Format, big-endian byte order
  • UTF-16LE
    Sixteen-bit UCS Transformation Format, little-endian byte order
  • UTF-16
    Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark
Abubakkar
  • 15,488
  • 8
  • 55
  • 83
0

Try this

Writer out = new BufferedWriter(new OutputStreamWriter(
    new FileOutputStream("outfilename"), "UTF-8"));
try {
    out.write(aString);
} finally {
    out.close();
}
Tuna
  • 2,937
  • 4
  • 37
  • 61
grepit
  • 21,260
  • 6
  • 105
  • 81
0

Since you already know that you should use a BufferedWriter since it let's you chose the encoding I do not see what's the problem here.

I would only like to add that instead of writing "UTF8" as a string which is error prone you can use, as of Java 1.7, the class StandardCharsets to get, well, all the standard charsets :-)

If Java 1.7 isn't an option you can also use Guava's Charsets class.

Mateusz Dymczyk
  • 14,969
  • 10
  • 59
  • 94