简体   繁体   English

如何为文件的每一行创建一个带有链表的最后一个元素的新目录

[英]How Can I create a new Directory with the last element of the linkedList for each line of the file

I have text file that contain 3 columns ID tweet Classification 我的文本文件包含3列ID tweet分类

I want to create a new Directory for each element of Classification so I have created a Linked List to Get Last index and create a directory but it dosent work can you help me fix it? 我想为分类的每个元素创建一个新目录,所以我创建了一个链接列表以获取上一个索引并创建了一个目录,但是它可以有效地解决我的问题吗? thanks this is my code: 谢谢这是我的代码:

//Create a linked list 
        LinkedList <String> fileLinkedList = new LinkedList<String>();
        fileLinkedList.add(line.toString());//line is a String variable that contains buff.readLine();
        System.out.println(fileLinkedList);

        for(int i = 0; i < fileLinkedList.size();i++){
            File LinkedFile = new File(fileLinkedList.getLast().toString());
            fileLinkedList.add(line);
            System.out.println(fileLinkedList);
            if(LinkedFile.mkdirs()){
                System.out.println("make dir");

            }else{
                System.out.println("doesnt work");
            }


        }

What I understand is you have a file that contains three columns like this : 据我了解,您有一个包含以下三列的文件:

ID1 tweet1 Classification1
ID2 tweet2 Classification2
ID3 tweet3 Classification3
ID4 tweet4 Classification4

You want to read lines of this file, get just Classification column and add them to linked list and then create a new directory the name of last element that is Classification 您要读取此文件的行,仅获得“ Classification列,并将其添加到链接列表中,然后创建一个新目录,其最后一个元素的名称为“ Classification

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader("File directory"));
LinkedList<String> fileLinkedList = new LinkedList<String>();
String line;
while ((line = bufferedReader.readLine()) != null) {
    fileLinkedList.add(line.split(" ")[2]);

    for(int i = 0; i < fileLinkedList.size();i++){
        File LinkedFile = new File(fileLinkedList.removeLast());

        if(LinkedFile.mkdirs()){
             System.out.println("make dir");

         }else{
             System.out.println("doesnt work");
         }
   }

}

And be careful, if you five just a name of directory in new File() it creates directories in your project directory of workspace. 并要小心,如果您仅在new File()一个目录名,它将在您工作区的项目目录中创建目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM