What´s in a string?
A string can be a single character, a word, or line of words. Strings might include line breaks:
String str = "This is the first string´s line \n" +
"plus this is the second string´s line \n" +
"etc ";
Writing a multi-line string to a file can be easily done by:
public void Writesourcedata(String pth) {
PrintWriter outputStream;
try {
outputStream = new PrintWriter (new FileWriter(pth));
outputStream.print(str);
}
outputStream.close();
etc.
The line breaks appear correctly inside the text.
But reading the file back into a application is troubling, the next
Code does put the string togther but ignores the linebreaks
private boolean readsourcedata(String path) { // the source file reader
try {
scanner = new Scanner( new FileReader(path) );
}
catch (FileNotFoundException e) {
System.out.println(path + " not present");
}
if (scanner==null) {
System.out.println(path + " can not read that file");
}
for ( i = 0; i < numberpic; i++) {
str = scanner.nextLine();
img[i].infotext = " ";
while (!str.equalsIgnoreCase("//*")) {
img[i].infotext = img[i].infotext.concat(" " + str);
str = scanner.next();
}
etc.
Each end of the string is marked by //* but the line breaks are not registered by this code. I tried
if (str.equalsIgnoreCase("\n"))
str = (str + " \n"); //(maybe a little silly to do)
but did not work. An other approach would be to read the whole line scanner.nextLine(); but than the problem would be, how to mark the end of the string block (//*).
Willemjav
(to string or to string)
