简体   繁体   中英

Can't write data to file

I'm new to this site, java and programming in generall. You'd say I'm a total noob. So here is my problem: I just started working with files, so I try various things for practice. Say i want to write a lot of numbers in a file. If I use this code, everything works fine:

    int value = 0;
    int max = 10000;
    try {
        FileWriter wr = new FileWriter("newFile.txt");
        for (int i = 0; i < max; i++){
            value = i;
            wr.write(value + " ");
        }
        wr.close();
    } catch (Exception e){
        println(e);
    }

newFile.txt is created with all the expected numbers. But if use this code to write randomly selected numbers from 0 to 9,

    int value = 0;
    int max = 10000;
    try {
        FileWriter wr = new FileWriter("newFile.txt");
        for (int i = 0; i < max; i++){
            value = RandomGenerator.getInstance().nextInt(0,9);
            wr.write(value + " ");
        }
        wr.close();
    } catch (Exception e){
        println(e);
    }

newFile.txt is created but is filled with << ‱‷‱‰′‱′‵‶‰′‸′‵‸‸″‰‰‵‶‵‱‶‵ >>.

Note that RandomGenerator is a class from acm package -a package that my study book uses in examples.

I've tried different classes like BufferedWriter and PrintWriter but I get the same output.

So, am I missing something here? I really can't figure it out.

Thank you.

EDIT: Thank you for your quick responses. I 've already tried to get the output via System.out.println alongside with wr.write . The screen output was as expected, but the file output was not. Something more odd occured though: I run it again but with a range from 0 to 10. The only thing I changed was this line:

value = RandomGenerator.getInstance().nextInt(0,10);

Now newFile.txt is as expected.. Any ideas why is that??

thanx

I think nothing is very wrong with your code.

A slightly altered version that works without your random library:

int max = 10000;
Random random = new Random();

try(FileWriter wr = new FileWriter("newFile.txt")) {
    for (int i = 0; i < max; i++){
        int value = random.nextInt(9);
        wr.write(value + " ");
    }
} catch (Exception e){
    e.printStackTrace();
}

When I ran the above, I got the expected file with lots of numbers from 0 to 8.

If your Random library does indeed return a int , you should definitely get the numbers in the file, even if in the wrong range.

Perhaps the problem is when you visualize the file? It might be encoding, given that this code uses your platform's default encoding (which may be very strange to cause this issue).

Try using this as a writer to ensure the file is written with UTF-8:

try(OutputStreamWriter wr = new OutputStreamWriter(
        new FileOutputStream("newFile.txt"), "UTF-8")) {
import java.io.FileWriter;
import java.util.Random;


public class Randomfile {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int value = 0;
        int max = 10000;
        try {
            Random randomGenerator = new Random();
            FileWriter wr = new FileWriter("D:\\newFile.txt");
            for (int i = 0; i < max; i++){
                value = randomGenerator.nextInt(10);
                wr.write(value + " ");
            }
           wr.close();
        } catch (Exception e){
            System.out.println("e:"+e);
        }
    }
 }

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