user2852379
user2852379

Reputation:

How to write an int value to a file?

i am trying to implement file handling into a small project i am currently working on. At the moment i can read in and write out an array of objects to an external .txt document but i am also trying to write out an int value which keeps track of the unique id of the last added element to an array List.

I am new to java, especially file handling. I am not sure if i can send in the int value along with the array list or if i need to create a new method and .txt document and write it to that. Below is what i have done so far, as you can see i have tried to send in the int with the array but this is as far as i can get.

public void writeToFile(List<? extends Serializable> team, int maleLastId) {
     try {

      outByteStream = new FileOutputStream(aFile);
      OOStream = new ObjectOutputStream(outByteStream);

      OOStream.writeObject(team);

      outByteStream.close();
      OOStream.close();
     } catch(IOException e) {
         JOptionPane.showMessageDialog(null,"I/O Error" + e + "\nPlease Contact your Administrator :-)");
     }
    }

Upvotes: 1

Views: 1122

Answers (1)

Arash Saidi
Arash Saidi

Reputation: 2238

A simple way to write to file is using the BufferedWriter class:

    int x = 10;
    BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter("utfil.txt", true));
        } catch (IOException e) {
            e.printStackTrace();
        }

    writer.write(x);

Upvotes: 1

Related Questions