1

I am having trouble figuring out how to read and write data to and from a file where my values are encapsulated in double quotations.

I am a novice programmer and confused on how to use the pattern regex to get around the issue and am hoping there is another way.

my data file I need to read from is formatted as follows:

type = "sedan"
productID = "000123"
price = "30000"

Also when I write to the file I need to follow the same format and I cannot figure out how to do it. So far I have the following:

PrintWriter fileWriter = new PrintWriter("products.txt", "UTF-8");
fileWriter.println("type = " + pTemp.getType + """);

which does not work either.

If anyone could help it would be greatly appreciated.

shmosel
  • 49,289
  • 6
  • 73
  • 138
Alex
  • 11
  • 1

2 Answers2

1

Just us the \ quote to escape the special character "

PrintWriter fileWriter = new PrintWriter("products.txt", "UTF-8");
fileWriter.println("type = \"" + pTemp.getType + "\"");
Jerin Joseph
  • 1,087
  • 9
  • 17
1

To use special characters in java and other similar languages, the usual way is to use a standard escape character \.

So for example your code to write the file shall be

PrintWriter fileWriter = new PrintWriter("products.txt", "UTF-8");
fileWriter.println("type = \"" + pTemp.getType + "\"");
Earth Engine
  • 10,048
  • 5
  • 48
  • 78