我需要能够比较文件的条目 - Java
I need to be able to compare entries of a file - Java
`导入java.io.BufferedReader;
导入 java.io.FileReader;
导入 java.io.IOException;
public class Dokimi {
private static String line;
public static void main (String[] args) throws IOException
{
int x = 0;
BufferedReader br = new BufferedReader(new FileReader("src/film.txt"));
line = br.readLine();
String[] filmline = new String [1000];
while (line != null) {
line = br.readLine();
filmline[x] = line;
x++;
}
br.close();
for (int i = 0; i<x; i++) // after many tries the last change I made is this. This is the testing class.
{
String [] arr = filmline[i].split(": ");
if ( i == x-1) // I know it isn't the best, maybe not even good but I tried many things and had nothing to lose.
{
for ( String ss : arr) {
String test = ss;
if (test.equals("Dancing With The Dogs "))
{
System.out.println("gotcha!");
}
}
}
}
}
}`因此,我有一个包含一些电影属性的文本文件。例如:
"film id : 1 film title : Pirates Of Hawai film category : action , comedy film description : A pirate from Hawai drinks rum and goes on an adventure to find more rum."
(一行中的每个条目)并且每次用户尝试添加新条目时,我都必须确保该电影尚未在文件中。我尝试了 slpit 方法(通过使用 ":"
和擦除 "film id" 等)和 StringTokenizer 但它只在一个上工作并由我指定行,而不是在循环中以便它可以读取整个文件。
不要使用 StringTokenizer,它是遗留的,出于维护原因应该得到支持,但不会在新代码中实现。
考虑到标记每次都不同,您可能希望 运行 遍历字符串并在这里和那里使用子字符串,也就是说,假设每行包含相同的标记。
或者,更改您的输入:
"1*Pirates Of Hawaiaction , comedyA pirate from Hawai drinks rum and
goes on an adventure to find more rum."
这样所有的token都是一样的,你就可以使用split方法了
根据here
稍微更改您的行以添加“:”:
"film id : 1 : film title : Pirates Of Hawai : film category : action , comedy : film description : A pirate from Hawai drinks rum and goes on an adventure to find more rum."
你可以试试这个方法并与你的比较:(在添加之前使用existsfilm验证它是否已经存在)
public void showAllFilms(){
ArrayList<String[]> films = getFilms();
for(String[] film : films){
System.out.println("id "+film[0]+"\ntitle "+film[1]);
}
}
public existsFilm(String filmName){
ArrayList<String[]> films = getFilms();
for(String[] film : films){
if(film[1].equals(filmName)){
return true;
}
}
return false;
}
public ArrayList<String[]> getFilms(){
ArrayList<String[]> filmList = new ArrayList();
int lineRead = 0;
try{
File file = new File("yourfile.txt");
BufferedReader br = new BufferedReader(new FileReader(file)));
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(":");
if(data.length > 0){
filmList.add(new String[]{data[1],data[3],data[5],data[7]});
}
lineRead++;
}
}catch(Exception ex){
System.out.println("Error reading line "+lineRead);
ex.printStackTrace(); //very ugly using this (common is logging it)
}
return filmList;
}
`导入java.io.BufferedReader; 导入 java.io.FileReader; 导入 java.io.IOException;
public class Dokimi {
private static String line;
public static void main (String[] args) throws IOException
{
int x = 0;
BufferedReader br = new BufferedReader(new FileReader("src/film.txt"));
line = br.readLine();
String[] filmline = new String [1000];
while (line != null) {
line = br.readLine();
filmline[x] = line;
x++;
}
br.close();
for (int i = 0; i<x; i++) // after many tries the last change I made is this. This is the testing class.
{
String [] arr = filmline[i].split(": ");
if ( i == x-1) // I know it isn't the best, maybe not even good but I tried many things and had nothing to lose.
{
for ( String ss : arr) {
String test = ss;
if (test.equals("Dancing With The Dogs "))
{
System.out.println("gotcha!");
}
}
}
}
}
}`因此,我有一个包含一些电影属性的文本文件。例如:
"film id : 1 film title : Pirates Of Hawai film category : action , comedy film description : A pirate from Hawai drinks rum and goes on an adventure to find more rum."
(一行中的每个条目)并且每次用户尝试添加新条目时,我都必须确保该电影尚未在文件中。我尝试了 slpit 方法(通过使用 ":"
和擦除 "film id" 等)和 StringTokenizer 但它只在一个上工作并由我指定行,而不是在循环中以便它可以读取整个文件。
不要使用 StringTokenizer,它是遗留的,出于维护原因应该得到支持,但不会在新代码中实现。
考虑到标记每次都不同,您可能希望 运行 遍历字符串并在这里和那里使用子字符串,也就是说,假设每行包含相同的标记。
或者,更改您的输入:
"1*Pirates Of Hawaiaction , comedyA pirate from Hawai drinks rum and goes on an adventure to find more rum."
这样所有的token都是一样的,你就可以使用split方法了
根据here
稍微更改您的行以添加“:”:
"film id : 1 : film title : Pirates Of Hawai : film category : action , comedy : film description : A pirate from Hawai drinks rum and goes on an adventure to find more rum."
你可以试试这个方法并与你的比较:(在添加之前使用existsfilm验证它是否已经存在)
public void showAllFilms(){
ArrayList<String[]> films = getFilms();
for(String[] film : films){
System.out.println("id "+film[0]+"\ntitle "+film[1]);
}
}
public existsFilm(String filmName){
ArrayList<String[]> films = getFilms();
for(String[] film : films){
if(film[1].equals(filmName)){
return true;
}
}
return false;
}
public ArrayList<String[]> getFilms(){
ArrayList<String[]> filmList = new ArrayList();
int lineRead = 0;
try{
File file = new File("yourfile.txt");
BufferedReader br = new BufferedReader(new FileReader(file)));
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(":");
if(data.length > 0){
filmList.add(new String[]{data[1],data[3],data[5],data[7]});
}
lineRead++;
}
}catch(Exception ex){
System.out.println("Error reading line "+lineRead);
ex.printStackTrace(); //very ugly using this (common is logging it)
}
return filmList;
}