0

Okay so I'm making a making a car parking system and am trying to store data into a pre-existing text file to read at a later date.

Storing the data works fine, using the code

Files.write(Paths.get("FilePath.txt"),"\r\nhello".getBytes(), StandardOpenOption.APPEND);`

However, when I try to store multiple data types for example:

Files.write(
  Paths.get(
    "C:\\Users\\A612475\\Desktop\\Project1\\TextFiles\\TicketData.txt"),
    "\r\nhello" + regNo.getBytes(), 
    StandardOpenOption.APPEND
  );`

with regNo being declared as a string elsewhere, I get the following message:

The method write(Path, byte[], OpenOption...) in the type Files is not applicable for the arguments (Path, String, StandardOpenOption)

I can store the data in the string by writing the File.write method out dozens of times, but is there a more practical way to do this?

EDIT: Solution was found here: How to append text to an existing file in Java

Community
  • 1
  • 1
94smitha
  • 31
  • 1
  • 6

1 Answers1

0

The problem here is simple, instead of passing a byte array you are passing a string. "\r\nhello" + regNo.getBytes() Is acts as a simple concatenation operation and thus what you are getting is a string, Thus this error.