简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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