我在这方面遇到了麻烦
//obj.generateIndexes(failuSarasas, 2);
HashMap<String, HashMap<String, Integer>> indTwoList = obj.generateIndexes(failuSarasas, 2);
obj.printHashMap(indTwoList);
//obj.generateIndexMatrix(indTwoList, failuSarasas);
和
HashMap<String, HashMap<String, Integer>> indexList = new HashMap<String, HashMap<String, Integer>>();
for (int i = 0; i < fileList.length; i++)
package modeliavimas2;
import java.io.*;
import java.util.*;
import org.apache.commons.io.FileUtils;
public class mainas
{
public static void main(String[] args) throws IOException
{
mainas obj = new mainas();
File[] failuSarasas = obj.getFileList();
//obj.generateIndexes(failuSarasas, 2);
HashMap<String, HashMap<String, Integer>> indTwoList = obj.generateIndexes(failuSarasas, 2);
obj.printHashMap(indTwoList);
//obj.generateIndexMatrix(indTwoList, failuSarasas);
Scanner scaner = new Scanner(System.in);
System.out.println("Iveskite termina arba 1, kad iseit");
while(true)
{
String phrase = scaner.nextLine();
if(phrase.equals("1"))
{
break;
}
obj.search(phrase, 2);
System.out.println("Iveskite termina arba 1, kad iseit");
}
scaner.close();
}
public File[] getFileList()
{
File folder = new File(
" C:\\Users\\Dell Pc\\Desktop\\inform\\Informacijos modeliavimas\\Informacijos modeliavimas\\ld2\\pages");
File[] listOfFiles = folder.listFiles();
return listOfFiles;
}
public HashMap<String, HashMap<String, Integer>> generateIndexes(File[] fileList, int phraseLen) throws IOException
{
HashMap<String, HashMap<String, Integer>> indexList = new HashMap<String, HashMap<String, Integer>>();
for (int i = 0; i < fileList.length; i++)
{
ArrayList<String> pageWords = new ArrayList<String>();
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
File file = fileList[i];
if (file.isFile() && file.getName().endsWith(".txt"))
{
String content = FileUtils.readFileToString(file, "UTF8");
for (String a : content.split(" "))
{
a = a.toLowerCase();
a = a.replaceAll("\\W|\\d|\\_", ""); // isvalyt visus
// neraidinius arba neskaicius
if (a.length() >= 3 && a.length() <= 15)
{
pageWords.add(a); //
}
}
if(phraseLen <= 1)
{
for(String a : pageWords)
{
if(!hashMap.containsKey(a))
{
hashMap.put(a, 1);
} else
{
hashMap.put(a, hashMap.get(a) + 1);
}
}
}
else
{
for(int j = phraseLen; j < pageWords.size(); j++)
{
String a = pageWords.get(j-1) + " "+ pageWords.get(j);
if(!hashMap.containsKey(a))
{
hashMap.put(a, 1);
}
else
{
hashMap.put(a, hashMap.get(a) + 1);
}
}
}
indexList.put(file.getName(), hashMap);
}
}
return indexList;
}
public void printHashMap(HashMap<String, HashMap<String, Integer>> indexList)
{
for (String a : indexList.keySet())
{
HashMap<String, Integer> map = indexList.get(a);
for (String b : map.keySet())
{
String key = b.toString();
String value = map.get(b).toString();
System.out.println(key + " " + value);
}
}
}
public void generateIndexMatrix(HashMap<String, HashMap<String, Integer>> indexList, File[] fileList)
throws FileNotFoundException, UnsupportedEncodingException
{
PrintWriter writer = new PrintWriter("debug2.txt", "UTF-8");
ArrayList<String> termList = new ArrayList<String>();
for (String a : indexList.keySet())
{
HashMap<String, Integer> map = indexList.get(a);
for (String b : map.keySet())
{
if (!termList.contains(b.toString()))
{
termList.add(b.toString());
}
}
}
String[][] matrix = new String[fileList.length+1][termList.size()+1];
int counter = 1;
for (String a : termList)
{
matrix[0][counter++] = a;
}
counter = 1;
for(File a : fileList)
{
matrix[counter++][0] = a.getName();
}
for(int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix[i].length; j++)
{
if(i > 0 && j > 0)
{
if(indexList.get(matrix[i][0]).get(matrix[0][j]) != null)
{
writer.print(indexList.get(matrix[i][0]).get(matrix[0][j]) + " ");
}
else
{
writer.print(0+" ");
}
}
else
{
writer.print(matrix[i][j]+" ");
}
}
writer.println();
}
writer.close();
}
public void search(String searchPhrase, int phraseLen) throws IOException
{
String[] searchTermsRaw = searchPhrase.split(" ");
ArrayList<String> searchTermsProc = new ArrayList<String>();
// apdorojam paieskos zodzius
if(phraseLen <= 1)
{
for(String a : searchTermsRaw)
{
a = a.toLowerCase();
a = a.replaceAll("\\W|\\d|\\_", "");
searchTermsProc.add(a);
}
}
else
{
for(int i = 1; i < searchTermsRaw.length; i++)
{
searchTermsRaw[i] = searchTermsRaw[i].replaceAll("\\W|\\d|\\_", "");
String a = searchTermsRaw[i - 1] + " " + searchTermsRaw[i];
a = a.toLowerCase();
searchTermsProc.add(a);
}
}
ArrayList<ArrayList<String>> indexMatrix = new ArrayList<ArrayList<String>>();
ArrayList<String> containingPages = new ArrayList<String>();
try (BufferedReader br = new BufferedReader(new FileReader("c:\\Users\\ASUSPC\\Documents\\Uni stuff\\Eclipse projects\\modeliavimas2\\debug2.txt"))) // irasyti indexu matricos faila
{
String line = br.readLine();
Boolean firstLine = true;
while (line != null)
{
ArrayList<String> list = new ArrayList<String>();
if(phraseLen <= 1)
{
for(String a : line.split(" "))
{
list.add(a);
}
}
else
{
String[] a = line.split(" ");
for(int i = 1; i < a.length; i++)
{
String temp = "";
if(firstLine)
{
if(i+1 < a.length)
{
temp = a[i] + " " + a[i + 1];
}
i++;
}
else
{
temp = a[i];
}
list.add(temp);
}
}
indexMatrix.add(list);
firstLine = false;
line = br.readLine();
}
}
for(int i = 1; i < indexMatrix.size(); i++)
{
int counter = 0;
for(String a : searchTermsProc)
{
int xpos = indexMatrix.get(0).indexOf(a);
if(xpos == -1)
{
continue;
}
if(Integer.parseInt(indexMatrix.get(i).get(xpos)) != 0)
{
counter++;
}
}
if(counter == searchTermsProc.size())
{
containingPages.add(String.valueOf(i));
}
counter = 0;
}
System.out.println("Rasti terminai rasti:");
for(String a: containingPages)
{
System.out.println(a);
}
if(containingPages.size() == 0)
{
System.out.println("Terminas nerastas nei viename dokumente");
}
}
}
答案 0 :(得分:0)
您的failuSarasas
对象为空,因为这段代码似乎返回null:
public File[] getFileList()
{
File folder = new File(
" C:\\Users\\Dell Pc\\Desktop\\inform\\Informacijos modeliavimas\\Informacijos modeliavimas\\ld2\\pages");
File[] listOfFiles = folder.listFiles();
return listOfFiles;
}
您甚至可以在使用新的File
对象执行任何操作之前检查文件是否存在。 File
类有一个exist()
方法,您可以使用它来做到这一点。祝你好运!
修改强> 每个Oracle文档...
public File [] listFiles()
返回一个抽象路径名数组,表示此抽象路径名表示的目录中的文件。
如果此抽象路径名不表示目录,则此方法返回null。否则返回一个File对象数组,一个用于目录中的每个文件或目录...
看到第三段?这就是为什么我怀疑你提供的路径不存在,这就是为什么我说要先通过File#exists()
方法进行检查。
以下是文档文档的链接:https://docs.oracle.com/javase/7/docs/api/java/io/File.html#listFiles()
P.S。除非您需要使用File
,否则我建议您使用Files
以及该程序包中的那些项目(java.nio.file
)。