简体   繁体   中英

Reading a file and storing strings into array

I'm trying to get some data from a file (MoviesList) and store them in variables and then printing them.

The file contains different paragraphs like this :

Fast & Furious7 (2015)
Type: Movie
Director : James Wan 
With : Vin Diesel, Paul Walker, Dwayne Johnson 
Action | Crime | Thriller 137 mins.

and this is a snippet from the code

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;


public class FlotTest {
    private static BufferedReader fR;


    public void lireLignes(File f) throws IOException{
         fR= new BufferedReader(new FileReader(f));
        String chaine ="";
         do {
         chaine = fR.readLine();
         if (chaine!= null) {

             String [] tab=chaine.split(","); 
             String MovieName=tab[1]; 

        System.out.println(MovieName);



    }
    }while (chaine != null);
         fR.close();

}
}

and this is the test class

package testFlot;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

public class test1 {
    public static void main(String[] args) throws IOException{
        FlotTest t= new FlotTest();
    File f = new File("MoviesList.txt");
       t.lireLignes(f);
    }

I'm guessing that you're stuck adding the MovieName to an array? If so...

Declare an ArrayList<String> variable (at the class level presuming you want access to it outside of the method), and where you're currently calling System.out.println , simply add them to the ArrayList variable.

If you have the line read and stored into String chaine then you could do:

   //String chaine="Fast & Furious7 (2015) Type: Movie Director : James Wan With : Vin Diesel, Paul Walker, Dwayne Johnson Action | Crime | Thriller 137 mins";

    String title = chaine.substring(0, chaine.indexOf("(")-1);

    System.out.println(title);

    String year = chaine.substring(chaine.indexOf("(")+1, chaine.indexOf(")"));

    System.out.println(year);

    String type = chaine.substring(chaine.indexOf(":")+2, chaine.indexOf("Director"));

    System.out.println(type);

    String directors = chaine.substring(chaine.indexOf("Director")+11, chaine.indexOf("With"));

    System.out.println(directors);

    //you'll have to figure out a way to parse the actors in depending on how your file is set up
    //it's interesting you don't have a seperator between Actors and the Genre


    //you could create an array
    String[] movie = new String[7];
    movie[0]= title;
    movie[1]=year;
    movie[2]=type;
    movie[3]=directors;



    //I find arraylists easier to deal with so you could create an arraylist
    ArrayList<String> movieList = new ArrayList<String>();
    movieList.add(title);
    movieList.add(year);
    movieList.add(type);
    movieList.add(type);
    movieList.add(directors);

I stopped because I have some work to finish but you could easily add similar lines for the actors and for the genre and for the time length and then add them either to the array or to the arrayList.

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