I have data file like:
First Name,Last Name,Number
Robert,Thomas
Jimmy,Johns
Robert,Thomas
Conner,Thomas
Robert,Thomas
Jimmy,Johns
Outfile Should be like:
First Name,Last Name,Number
Robert,Thomas,1
Robert,Thomas,2
Robert,Thomas,3
Jimmy,Johns,1
Jimmy,Johns,2
Conner,Thomas,
我编写了代码,但实际上它正在删除那些重复数据,但我想在最后的字段中写入数字。
这是我的代码:
import java.io.File;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JOptionPane;
public class Data_Prospect_key1 {
public static void delFileFromDir(String dirPath) {
File dir = new File(dirPath);
if (dir.listFiles() == null)
return;
for (File file : dir.listFiles()) {
if (!file.isDirectory())
file.delete();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
BufferedReader br = null;
BufferedWriter bfAlldcount = null;
File folderall = new File("Data_File_IN");
File[] BFFileall = folderall.listFiles();
for (File file : BFFileall) {
String str = file.getName();
br = new BufferedReader(new FileReader(file));
String count = "Data_File_OUT\\" + "OUT_" + str;
bfAlldcount = new BufferedWriter(new FileWriter(count));
String line;
line = br.readLine();
Map<String, String> maps = new HashMap<String, String>();
while ((line = br.readLine()) != null) {
String[] actionID = line.split("\\,");
String PFN = actionID[0].trim();
String PLN = actionID[1].trim();
maps.put(PFN, PLN);
}
for (Map.Entry<String, String> entry : maps.entrySet()) {
System.out.println("First Name: -" + entry.getKey() + " , Last Name="
+ entry.getValue());
}
}
} catch(Exception lettercount) {
lettercount.printStackTrace();
}
}
}
我的代码输出只是删除重复.. 输出是:
Robert,Thomas
Jimmy,Johns
Robert,Thomas
请帮助我,我怎么能这样做...请...感谢Advaced !!
答案 0 :(得分:0)
以下是一些指示
喜欢这个。
package se.wederbrand.stackoverflow;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class CountingRobert {
public static void main(String[] args) {
Map<String, Integer> names = new HashMap<>();
Scanner scanner;
if (args.length == 0) {
// for testing
String input = "First Name,Last Name,Number\n" +
"Robert,Thomas\n" +
"Jimmy,Johns\n" +
"Robert,Thomas\n" +
"Conner,Thomas\n" +
"Robert,Thomas\n" +
"Jimmy,Johns";
scanner = new Scanner(input);
}
else {
scanner = new Scanner(args[0]);
}
// Skip one line with the heading
scanner.nextLine();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (!names.containsKey(line)) {
names.put(line, 0);
}
names.put(line, names.get(line) + 1);
}
for (String name : names.keySet()) {
for (int i = 1; i <= names.get(name); i++) {
System.out.println(name + " " + i);
}
}
}
}
答案 1 :(得分:0)
我对这种方法感到困惑。 您的文件包含以下格式:
Robert,Thomas
Jimmy,Johns
Robert,Thomas
Conner,Thomas
Robert,Thomas
Jimmy,Johns
并且您想要获取名字和姓氏列表(没有重复)。
所以你可以做的是逐行读取文件并将FS,PS放在Map中并按照流程直到EOF。由于MAP具有不存在重复键的属性,因此您将获得所需的输出
Robert,Thomas
Jimmy,Johns
Conner,Thomas