1

I have program that reads user input and then writes to file. After that program reads that file and make some basic arithmetic functions. Then results are shown on screen for user.After that I want to clear that file, because it will be like cache for program, no need for permanent store.

It all works great, and I can clear file, but I got such weird exception:

java.io.UnsupportedEncodingException And program stops.

My code: The file looks like this

2013      Jūnijs              1500.0              80                  125                 293.7               151.25              1055.05             
2013      Jūlijs              1150.0              80                  125                 218.94              112.75              818.31              
2013      Septembris          1550.0              80                  125                 304.38              156.75              1088.87   

Clearing the file is done with this code :

 public static void Clear_file() throws IOException{
                 System.out.println("Notīram failu");
                 clear = new Formatter(new FileWriter(user_name()+".txt", true));
                 FileOutputStream erasor = new FileOutputStream(user_name()+".txt");
                 erasor.write((new String().getBytes("")));
                 erasor.close();             
             }

I read the guide and there are written like this : If the given charset is not in that list then it is certain that this error will be thrown.

I am confused, because in the file are just String and double type data.

How can I avoid trowing this exception?

Thanks :)

1
  • out of curiosity, what do you actually expect this line erasor.write((new String().getBytes(""))); to do? Commented Nov 4, 2013 at 18:41

2 Answers 2

3

new String().getBytes("")

You provided no name for the charset, that's why the exception is thrown.

Try to set one and you'll see that it runs correctly.

System.out.println(Arrays.toString(new String("test").getBytes("UTF-8")));

Output :

[116, 101, 115, 116]

Sign up to request clarification or add additional context in comments.

7 Comments

Oooh! We should check the time in milliseconds to compare :P
Is that possible on SO?
@MartijnCourteaux I don't think so
@MartijnCourteaux Well, you can try to build a query at data.stackexchange.com/stackoverflow/queries But the datetime is stored as YYYY-MM-DD HH:MM:SS, so I think you won't get any further informations. You got my +1 too =)
Nice, I found it! A datetime is stored as a long which expresses the number of milliseconds since epoch. But there is some latency on the server. The caches are not ready yet to answer the query that will tell us who answered first on this question. For older questions it works. data.stackexchange.com/stackoverflow/query/146189/…
|
1
erasor.write((new String().getBytes("")));

Here, you ask the empty String object to get a byte array encoded in the encoding called: "". (no name). Of course, there is no character encoding that is named "".

To clear the file, use something like:

new FileOuputStream(file).close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.