我有文本文件。
每个文本文件都包含一些文件夹名称。
例如,
album.txt包含
@事件1
@事件2
@ EVENT3
和new_album.txt包含
@事件1(update20-05-2015)
@事件2(update03-03-2016)
@ EVENT3(update15-08-2016)
@ EVENT4(update30-07-2017)
我想逐行比较album.txt中的相似文件夹名称和new_album.txt,然后将与album.txt相似的文件夹名称放入similar.txt,并将文件夹名称与not_match.txt不匹配。
在similar.txt输出
@事件1
@事件2
@ EVENT3
not_match.txt中的输出
@ EVENT4(update30-07-2017)
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class CompareFileName {
public static void main(String[] args) throws Exception {
BufferedReader br1 = null;
BufferedReader br2 = null;
String sCurrentLine;
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
br1 = new BufferedReader(new FileReader("album.txt"));
br2 = new BufferedReader(new FileReader("new_album.txt"));
while ((sCurrentLine = br1.readLine()) != null) {
list1.add(sCurrentLine);
}
while ((sCurrentLine = br2.readLine()) != null) {
list2.add(sCurrentLine);
}
//This part is my problem
List <String> list_similar = new ArrayList<String>();
List <String> list_not_match = new ArrayList<String>();
for (String string : list1) {
if(string.matches("list2")){ //I don't know how to compare similar folder name from list2 with list1.
list_similar.add(string);
}else{list_not_match.add(string)}
}
//这部分是我用于将字符串添加到文本文件的部分,但它不完整我想将list_similar中的字符串写入similar.text并将list_not_match写入not_match.txt
file = new File("similar.txt");
fileName = "similar.txt";
str = file.list();
try{
PrintWriter outputStream = new PrintWriter(fileName);
for(String string:str){
outputStream.println(string);
}
outputStream.close();
System.out.println("get name complete");
}
catch (FileNotFoundException e){
e.printStackTrace();
}
System.out.println("done.");
}
答案 0 :(得分:1)
如果您想从文件中读取内容,可以使用这些流。
na.locf
如果您想阅读文件夹名称,可以使用它。
<Route path="/:slug" component={ItemPage} />
如果要创建新文件或文件夹,可以使用此功能。
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(new File("path\\to\\your\\file.txt"))); //or any format
BufferedWriter writer = new BufferedWriter(new FileWriter(new File("path\\to\\your\\second\\file.txt")));
//read one line from your file
String line = reader.readLine();
//write something to your file
writer.write(line);
}
答案 1 :(得分:0)
public class Application {
public static void main(String... aArgs) throws IOException {
InputParser firstListInputParser = new InputParser(new File(/**"Your path to /album.txt"*/));
firstListInputParser.processLineByLine();
List<String> firstList = firstListInputParser.getListWithParsedFolderNames();
firstListInputParser.printMap();
InputParser secondListInputParser = new InputParser(new File(/**"Your path to /new_album.txt"*/));
secondListInputParser.processLineByLine();
List<String> secondList = secondListInputParser.getListWithParsedFolderNames();
secondListInputParser.printMap();
// Create the list with common value and write it to the file
List<String> listWithCommonValues = new ArrayList<String>(firstList);
listWithCommonValues.retainAll(secondList);
Path fileCommon = Paths.get(/**"Your path to /similar.txt"*/);
Files.write(fileCommon, listWithCommonValues, Charset.forName("UTF-8"));
// Create the list with different values and write it to the file
List<String> listWithAllValues = new ArrayList<String>(firstList);
listWithAllValues.addAll(secondList);
//remove the common values from the list with all values
listWithAllValues.removeAll(listWithCommonValues);
Path fileDistincts = Paths.get(/**"Your path to /not_match.txt"*/);
Files.write(fileDistincts, listWithAllValues, Charset.forName("UTF-8"));
}
private static void log(Object aObject){
System.out.println(String.valueOf(aObject));
}
}
/**
* Assumes UTF-8 encoding
*/
public class InputParser {
//create a list to hold the values
List<String> listWithParsedFolderNames = new ArrayList<>();
//private final Path fFilePath;
private final File file;
private final static Charset ENCODING = StandardCharsets.UTF_8;
/**
Constructor.
@param aFileName full name of an existing, readable file.
*/
public InputParser(File aFileName){
//fFilePath = Paths.get(aFileName);
file = aFileName;
}
/**
* Processes each line and calls {@link #processLine(String)}}.
*/
public final void processLineByLine() throws IOException {
try (Scanner fileScanner = new Scanner(file, ENCODING.name())){
while (fileScanner.hasNextLine()){
processLine(fileScanner.nextLine());
}
}
}
/**
Overridable method for processing lines in different ways.
*Parses the line and cuts away the part after '(update'
* Ex1: input line: @Event1(update20-05-2015)
* Ex1: output : @Event1
*
* Ex2: input line: @Event2
* Ex2: output : @Event2
*/
protected void processLine(String aLine){
Scanner scanner = new Scanner(aLine);
if (scanner.hasNext()) {
String name = scanner.next();
String finalName = name.split("\\(update")[0];
//stores the values in the list
listWithParsedFolderNames.add(finalName);
} else {
log("Empty or invalid line. Unable to process.");
}
}
/**
* Prints the content of the listWlistWithParsedFolderNames
*/
public void printMap() {
Iterator it = listWithParsedFolderNames.iterator();
while (it.hasNext()) {
log("The prsed value is: " + it.next());
}
}
/**
* @return the list with values
*/
public List<String > getListWithParsedFolderNames() {
return this.listWithParsedFolderNames;
}
private static void log(Object aObject){
System.out.println(String.valueOf(aObject));
}
}
在similar.txt中,它将打印:
@Event1
@Event2
@Event3
在not_match.txt中,它将打印:
@Event4
如果您希望将@ Event4(update30-07-2017)打印到not_match类中,则必须将列表更改为键值映射,其中已解析的输入@ Event4为键,而整行@ Event4( update30-07-2017)作为价值。比较地图的键后,您可以将值写入文件中。