Java 将带有换行符的字符串拆分为数组,其中使用缓冲 reader 从文件中读取字符串
Java splitting a string with newlines into an array, where the string is read from a file using buffered reader
我正在将一个文件加载到一个缓冲区中,该缓冲区被加载到一个字符串缓冲区中。然后我将这个字符串缓冲区复制到一个字符串中。字符串打印为...
Sam
ravon
Ashley
annie
所以我想为这个字符串创建一个数组,这样我就可以一次将第一行和第二行放入一个函数中,该函数为一个接受用户名和密码的 LinkedList 创建一个节点...例如用户名:山姆,密码:ravon。然后将其加载到 LinkedList 中。当涉及到 LinkedList 时,我的所有功能都在工作,但我似乎无法将我的字符串拆分为数组。
我想像...
String[] userContent = content.split("\n")
会将内容的每个元素放入 userContent[n] 字符串中,其中
userContent[0] = Sam, userContent[1] = ravon
等等。然而,事实并非如此。
我正在使用的代码 - 它以链表和文件名作为参数
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();
}
}
现在您只检查空格,
split("\n")
使用 %%n 作为新行,
或者你可以使用 %%W
这不是字母数字字符
快乐编码
以下内容的价值与您的代码在没有 creating/splitting String 和不必要地重新打开文件的情况下所做的相同,并且如果您碰巧 运行 也不会抛出异常进入一个有用户但没有密码的文件。
BufferedReader br = new BufferedReader(new FileReader(fName));
String user;
while ((user = br.readLine()) != null) {
String pass = br.readLine();
if (pass == null) {
System.out.println("Warning: User found with no password: " + user);
break;
}
ll.add(new DataUser(user, pass));
}
我正在将一个文件加载到一个缓冲区中,该缓冲区被加载到一个字符串缓冲区中。然后我将这个字符串缓冲区复制到一个字符串中。字符串打印为...
Sam
ravon
Ashley
annie
所以我想为这个字符串创建一个数组,这样我就可以一次将第一行和第二行放入一个函数中,该函数为一个接受用户名和密码的 LinkedList 创建一个节点...例如用户名:山姆,密码:ravon。然后将其加载到 LinkedList 中。当涉及到 LinkedList 时,我的所有功能都在工作,但我似乎无法将我的字符串拆分为数组。
我想像...
String[] userContent = content.split("\n")
会将内容的每个元素放入 userContent[n] 字符串中,其中
userContent[0] = Sam, userContent[1] = ravon
等等。然而,事实并非如此。
我正在使用的代码 - 它以链表和文件名作为参数
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();
}
}
现在您只检查空格,
split("\n")
使用 %%n 作为新行, 或者你可以使用 %%W 这不是字母数字字符 快乐编码
以下内容的价值与您的代码在没有 creating/splitting String 和不必要地重新打开文件的情况下所做的相同,并且如果您碰巧 运行 也不会抛出异常进入一个有用户但没有密码的文件。
BufferedReader br = new BufferedReader(new FileReader(fName));
String user;
while ((user = br.readLine()) != null) {
String pass = br.readLine();
if (pass == null) {
System.out.println("Warning: User found with no password: " + user);
break;
}
ll.add(new DataUser(user, pass));
}