Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
错误地逐行读取java中的.txt文件_Java_File_Netbeans - Fatal编程技术网

错误地逐行读取java中的.txt文件

错误地逐行读取java中的.txt文件,java,file,netbeans,Java,File,Netbeans,我试图用java读取一个.txt文件,并创建一个列表列表,以便将该.txt文件的每一行放到另一个列表中。对于我尝试做的每一个文件,一切都很好,但是对于facebook_combined.txt.gz文件,它没有正确的方法。 例如: 如果另一个.txt文件的第一行是这样的 529945617045第二个是这样的 70 80 65 91那么我的代码应该创建名为行的列表,行必须如下所示: line=[[52,99,45,61,70,45][70,80,65,91]]. lines=[[0,1][0,

我试图用java读取一个.txt文件,并创建一个列表列表,以便将该.txt文件的每一行放到另一个列表中。对于我尝试做的每一个文件,一切都很好,但是对于facebook_combined.txt.gz文件,它没有正确的方法。 例如:

如果另一个.txt文件的第一行是这样的
529945617045
第二个是这样的
70 80 65 91
那么我的代码应该创建名为行的列表,行必须如下所示:

line=[[52,99,45,61,70,45][70,80,65,91]].
lines=[[0,1][0,2][0,3][0,4][0,5][0,...]].
但是对于facebook_combined.txt文件,如果我们假设它的第一行是这样的
0 10 20 30 40 50
相同的代码创建列表行,如下所示:

line=[[52,99,45,61,70,45][70,80,65,91]].
lines=[[0,1][0,2][0,3][0,4][0,5][0,...]].
我使用的代码如下:

 ArrayList<ArrayList<String>> lines = new ArrayList<ArrayList<String>>();

//read the file
FileInputStream fstream = new FileInputStream("C:\\Users\\facebook_combined.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));

while (true)//while the file was read
{
    String line = br.readLine();//split the file into the lines
    if (line == null) 
    {
        break;//if there are no more lines left
    }

    Scanner tokenize = new Scanner(line);// split the lines into tokens and make into an arraylist
    ArrayList<String> tokens = new ArrayList<String>();

    while (tokenize.hasNext()) //while there are still more
    {
        tokens.add(tokenize.next());
    }
    lines.add(tokens);
}
    br.close();
ArrayList line=new ArrayList();
//读文件
FileInputStream fstream=新的FileInputStream(“C:\\Users\\facebook\u combined.txt”);
DataInputStream in=新的DataInputStream(fstream);
BufferedReader br=新的BufferedReader(新的InputStreamReader(in));
while(true)//读取文件时
{
String line=br.readLine();//将文件拆分为多行
如果(行==null)
{
break;//如果没有多余的行了
}
Scanner tokenize=new Scanner(line);//将行拆分为令牌并生成一个arraylist
ArrayList标记=新的ArrayList();
while(tokenize.hasNext())//还有更多
{
add(tokenize.next());
}
行。添加(令牌);
}
br.close();

我下载了数据集并用7Zip提取了文本文件,看起来您的程序正在运行。提取文件时,数据如下所示(使用记事本++)

我用普通记事本打开文件,回车不可见,因此可能导致混淆(即数据看起来像记事本中的
0 10 20 30 40…


编辑:更新的解释

回应OP


数据在notepad++中的显示方式是正确的,但是 右边的版本是0 10 20 30

我不确定这是否正确。注意,您假设数据应该被解析,即使文件提供非常明确的回车。如果文件不应该包含回车符,它就不会包含回车符。类似地,文件的格式似乎没有错误,因为格式始终是一对数字,后跟回车符没有任何东西指向被解析为
01003040的数据

文件facebook_combined.txt看起来是一个图表中的边列表,每个边都是两个人之间的友谊

看起来你正在尝试阅读朋友的“圈”,圈是一个数字列表。如果您下载另一个tar文件“facebook.tar”,则会有几个扩展名为*.circles的文件。下面是其中一个文件的片段

circle0 71  215 54  61  298 229 81  253 193 97  264 29  132 110 163 259 183 334 245 222
circle1 173
circle2 155 99  327 140 116 147 144 150 270
circle3 51  83  237
circle4 125 344 295 257 55  122 223 59  268 280 84  156 258 236 250 239 69
circle5 23
circle6 337 289 93  17  111 52  137 343 192 35  326 310 214 32  115 321 209 312 41  20

这些*.circles文件似乎是您期望的格式(数字列表)

我觉得你的代码有点错误。我通常不用“扫描器”。但也许您可以使用.split()

我不喜欢“while(true)”循环,因此我建议将其更改为:

String s;
while ((s = br.readLine()) != null) {
并删除您的:

String line = br.readLine();//split the file into the lines
if (line == null) 
{
    break;//if there are no more lines left
}
然后尝试使用类似以下内容的拆分:

String[] tokenize = line.split(" ");
ArrayList<String> tokens = new ArrayList<String>();
for(String s : tokenize){
tokens.add(s);
}
String[]tokenize=line.split(“”);
ArrayList标记=新的ArrayList();
for(字符串s:tokenize){
代币。添加;
}

好吧,您只需说.txt文件实际上看起来像

0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
但你需要像这样

   0 10 20 30 40 50

因此,我认为您需要读取所有文件,然后替换回车符

您可以使用
Files.readAllLines(您的文件路径)
来解决此问题。谢谢,但它不起作用。它再次向我返回如下行的列表=[[0,1][0,2][0,3][0,4][0,5][0,…]]。您在notepad++中的数据外观是正确的,但正确的版本是0 10 20 30如何解释数据?这应该是一张facebook好友图,对吗?那么01将是0和1之间的友谊。那么02将是0和2之间的友谊。等等。您如何将数据[0 10 20 30 40…]解释为朋友图?关于*.circle文件,您是对的。这些文件就是我要找的。谢谢。我在一小时前刚刚发布了另一个答案,它有效吗?谢谢,但它没有帮助,我倾向于认为文件是问题所在,而不是我管理它的方式。我不理解你的想法,你说的替换回车是什么意思?是的,使用.replace(“,”)方法。您可以在这里看到更多信息:现在您将知道如何替换它们。如果你不明白,请告诉我。