I'm loading a file into a buffer which is loaded into a string buffer. I'm then copying this string buffer into a string. The string prints out as...
Sam
ravon
Ashley
annie
So I want to make an array for this string so I can put the first line and second line at a time into a function that creates a node for a LinkedList that takes a username and password ... ex Username: Sam , Password: ravon. This is then loaded into a LinkedList. All of my functionally is working when it comes to the LinkedList, but I can't seem to split my string into an array.
I thought something like...
String[] userContent = content.split("\n") would place each element of content into userContent[n] string where
userContent[0] = Sam, userContent[1] = ravon and etc. However, this is not the case.
code I'm using - It takes a linked list and file name as arguments
public static void readUserFile(String fName, LinkedList<dataUser> ll) {
try {
File file = new File(fName);
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
LineNumberReader lnr = new LineNumberReader(new FileReader(new File("userData.txt")));
lnr.skip(Long.MAX_VALUE);
lnr.close();
fileReader.close();
int realSize = lnr.getLineNumber();
//String[] userContent = line.split("\n");
String content = stringBuffer.toString();
String[] userContent = content.split("/n");
//this prints nothing, would expect it to print exactly what content prints
for(int i = 0; i < realSize; i++) {
System.out.println(userContent[i]);
}
/* this is what I want to load the userContent string into
//not working
for(int i = 0; i < realSize; i++) {
dataUser tempUser = new dataUser(userContent[i], userContent[i+1]);
ll.add(tempUser);
i = i + 1;
}*/
//System.out.println(content); //works and prints the file with new lines
//System.out.println("Contents of file:");
//System.out.println(stringBuffer.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
java.nio.file.Files.readAllLinesis probably the way to go.ArrayListof strings while you are reading instead of appending to aStringBuffer?/ninstead of\n.