读取文件并将字符串存储到数组中

时间:2015-05-14 18:56:02

标签: java arrays java.util.scanner

我试图从文件中获取一些数据(MoviesList)并将它们存储在变量中,然后打印它们。

该文件包含不同的段落,如下所示:

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

这是代码

的摘录
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();

}
}

这是测试类

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);
    }

2 个答案:

答案 0 :(得分:0)

我猜你没有把unsigned distDIJKSTRA(unsigned vert1, unsigned vert2) { vector<Vertex3*> pq; Vertex3* v; unsigned distance = UINT_MAX; for (unsigned i = 0; i < vertices.size(); i++) { if (vertices[i]->value == vert1) v = new Vertex3(vertices[i], 0); else v = new Vertex3(vertices[i], distance); pq.push_back(v); } make_heap(pq.begin(), pq.end(), lowerPrior); unsigned newDist; while (!pq.empty()) { Vertex3* currV = pq.front(); // If the destination is reached if (currV->vert->value == vert2) return currV->dist; pop_heap(pq.begin(), pq.end(), lowerPrior); pq.pop_back(); // Checking if the route through currV is shorter for (unsigned i = 0; i < currV->vert->adjList.size(); i++) { Vertex3* nextV = nullptr; for (unsigned j = 0; j < pq.size(); j++) if (pq[j]->vert == currV->vert->adjList[i]) { nextV = pq[j]; break; } if (nextV != nullptr) { newDist = currV->dist + getWeight(currV->vert, nextV->vert); if (newDist < nextV->dist) nextV->dist = newDist; } else continue; } sort_heap(pq.begin(), pq.end(), lowerPrior); } return UINT_MAX; } 添加到数组中?如果是的话......

声明MovieName变量(在类级别,假设您希望在方法之外访问它),以及您当前正在调用ArrayList<String>的位置,只需将它们添加到System.out.println变量。

答案 1 :(得分:0)

如果您已阅读该行并将其存储到String chaine中,那么您可以执行以下操作:

   //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);

我停下来因为我有一些工作要完成,但你可以轻松地为演员,类型和时间长度添加相似的行,然后将它们添加到数组或arrayList。