我試着問在這裏找到我的問題一個一個解決方案:
Hashmap single key holding a class. count the key and retrieve counter
,然後看到你的questin,讓我評論它,看我怎麼解決你的問題。請給我反饋。
所以首先讓我們看看我們所要做的:
- 設置你的文件在它(OriginalDocFolder)
- 設置路徑爲存儲原始文件夾的路徑(TargetDocFolder)
路徑這兩個文件夾,並執行以下操作:個ReadAllFilesInOrignalFolder和CountAndWriteSeparateFilesInTargetFolders
我假設你的輸入文件都是這樣命名* 00001.txt」與其他的格式如下聰明的讓我知道要更新我的一些代碼爲你。
experiment
investig
aerodynam
wing
slipstream
brenckman
讓我改變主類中的以下內容:
,我在一個叫FileManipulation後來,我們在這裏調用函數從該類類寫作一切在你的主要班級。 (A)我在我的項目中添加了一個包,並將其命名爲ed.unlv.filemanimulation 然後(B)添加了一個名爲FileManipulation的類。java的到packacge這裏導入它由以下
import edu.unlv.filemanipulation.*;
import java.util.ArrayList;
import edu.unlv.utility.*;
/**
* @author Dara Nyknahad
*/
public class HashMapCount{
public static void main(String[] args) throws Exception {
//creating output folders
// to do: err handling if the folders exist
FileManipulation.createOutputFoldersAt("C:\\Dara\\Data\\Output\\");
String OriginalDocFolderPath="C:\\Dara\\Data\\Output\\Original\\";
String frequenceyDocFolderPath="C:\\Dara\\Data\\Output\\Frequency\\";
ArrayList<String> listOfOriginalFiles=new ArrayList<>();
FileManipulation.getFilePathFrom(OriginalDocFolderPath, listOfOriginalFiles);
String [] arrayOfOriginalFileAddresses = listOfOriginalFiles.toArray(new String[listOfOriginalFiles.size()]);
//Count the words
FileManipulation.countDistinctWordVersion1(arrayOfOriginalFileAddresses, frequenceyDocFolderPath);
}
}
現在,你可以看到我們改變了countDistinctWord的簽名接收陣列包含所有文件和我們將其設置爲目標的文件夾。
這裏我正在給你寫上面的想法和方法FileManipulation class。
注:以下代碼基於JDk8也NIO,所以你需要使用java8。
package edu.unlv.filemanipulation;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.Set;
/**
* @author Dara Nyknahad
*/
public class FileManipulation {
public static void getFilePathFrom(String FolderPath, ArrayList<String> listOfFiles){
//getting a folder filling an array that include the address of the files
try {
Files.walk(Paths.get(FolderPath)).forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
listOfFiles.add(filePath.toString());
//System.out.println("Reading file: "+ filePath);
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void createOutputFoldersAt(String outputFolder) throws Exception{
//creating a desired folder
Path frequnceyFolder=FileSystems.getDefault().getPath(outputFolder+"Frequency");
try {
Files.createDirectory(frequnceyFolder);
System.out.println("Frequency folder created");
} catch (IOException e) {
System.err.println(e.toString());
}
}
public static void countDistinctWordVersion1(String[] arrayOfStemmedFileAddresses, String frequenceyDocFolderPath) throws Exception{
System.out.println("Counting the words are in progress ...");
for(int i=0;i<arrayOfStemmedFileAddresses.length;i++){
Path inputPath = Paths.get(arrayOfStemmedFileAddresses[i]);
String idString=String.format("%05d", (i+1));
String outputPath=frequenceyDocFolderPath+idString+".txt";
// do your counting here
writeMapToFile(uniqueTreeSortedMap, idString, outputPath);
}
System.out.println("Counting is Done");
}
public static void writeMapToFile(Map<String,Integer> map, String idString, String outputPath) throws FileNotFoundException{
PrintWriter writer = new PrintWriter(outputPath);
Set s = map.entrySet();
Iterator it = s.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String key = (String) entry.getKey();
Integer value = (Integer) entry.getValue();
//System.out.println(key + "," + value);
writer.println(idString+","+key + "," + value);
}//while
writer.close();
}
}
運行後我得到這些結果在FrequenceyFolder,每個文件的輸出將導致一個單獨的文件。 (每個命名00001.txt,00002.txt ...)具有以下格式:
在FrequenceyFolder: 00001.txt包括:
00001,aerodynam,2
00001,agre,3
00001,angl,1
00001,attack,7
00001,basi,4
....
in FrequenceyFolder: 00999.txt包括:
在
00999,aerodynam,5
00999,evalu,1
00999,lift,3
00999,ratio,2
00999,result,9
....
FrequenceyFolder: 01400.txt包括:
01400,subtract,1
01400,support,1
01400,theoret,1
01400,theori,1
01400,.....
這是我從你的問題的理解。讓我知道。
分享你的努力 –
Svarog,幫助你一個樣本 –
我想爲所有文件使用單個hasmap。那麼在我的代碼片段中,我在哪裏使用@svarog建議的Iterator進行文件遍歷? –