无法在Java中读取csv文件中的下一行和剩余行
我试图为第[1]行(即实体名称)找到10多个重复项,但输出仅显示实体名称=“Rick Ross”的重复项,它不会读取并循环实体名称“Rick Ross”后的下一行 代码如下:无法在Java中读取csv文件中的下一行和剩余行,java,csv,arraylist,Java,Csv,Arraylist,我试图为第[1]行(即实体名称)找到10多个重复项,但输出仅显示实体名称=“Rick Ross”的重复项,它不会读取并循环实体名称“Rick Ross”后的下一行 代码如下: public class EventDetectioncopy { public static void main(String[] args) throws FileNotFoundException, IOException{ System.out.print("Enter a name for
public class EventDetectioncopy {
public static void main(String[] args) throws FileNotFoundException, IOException{
System.out.print("Enter a name for new Tweet Cluster (csv file): ");
BufferedReader scanFile = new BufferedReader(new InputStreamReader(System.in));
String newFile = scanFile.readLine();
try {
eventDetection(newFile);
}
catch (FileNotFoundException e) {
System.out.println(e);
}
catch (IOException e){
}
}
public static void eventDetection(String filename) throws FileNotFoundException, IOException{
String csv = "1day/clusters.sortedby.clusterid.csv";
FileWriter newCsv = new FileWriter(filename + "." + "csv");
BufferedWriter newCsvBW = new BufferedWriter(newCsv);
BufferedReader reader = new BufferedReader(new FileReader(csv));
String data;
try{
String temp = null;
List<String> tempList = new ArrayList<String>();
List<String> list = new ArrayList<String>();
while((data = reader.readLine()) != null)
{
String[] splitText = data.split(",");
//list = Arrays.asList(splitText);
String nameEntity = splitText[1];
if(temp != null)
{
if(!(nameEntity.equals(temp)))
{
if(tempList.size() >= 10)
{
for(int i = 0; i < tempList.size(); i ++)
{
newCsvBW.append(tempList.get(i));
newCsvBW.append("\n");
System.out.println(tempList.get(i));
}
break;
}
}
else
{
tempList.add(data);
}
}
else
{
temp = nameEntity;
tempList.add(data);
}
}
}
finally
{
reader.close();
newCsvBW.close();
}
}
}
在第一个实体名称结束后,如何读取csv文件中第[1]行的下一行。在该if块的末尾
if(!(nameEntity.equals(temp)))
在for循环之后添加以下内容:
temp = nameEntity;
tempList.add(data);
编辑:
根据comment的输入,将while循环替换为以下do-while:
do {
data = reader.readLine();
String nameEntity = null;
if (data != null) {
String[] splitText = data.split(",");
nameEntity = splitText[1];
}
if (temp != null) {
if (data == null || !(nameEntity.equals(temp))) {
if (tempList.size() >= 10) {
for (int i = 0; i < tempList.size(); i++) {
newCsvBW.append(tempList.get(i));
newCsvBW.append("\n");
System.out.println(tempList.get(i));
}
}
tempList.clear();
temp = nameEntity;
}
} else {
temp = nameEntity;
}
tempList.add(data);
} while (data != null);
do{
data=reader.readLine();
字符串nameEntity=null;
如果(数据!=null){
String[]splitText=data.split(“,”);
nameEntity=splitText[1];
}
如果(温度!=null){
if(data==null | |!(nameEntity.equals(temp))){
if(templast.size()>=10){
对于(int i=0;i
您的csv文件中的内容是什么?@Ravik我已经添加了我的csv文件的一些内容,它打印了“Rick Ross”并在后面加了一行,但没有继续阅读其余的内容。我已经在问题中添加了一些csv内容编辑了答案。试试这些变化,不客气。您所犯的主要错误是在将副本写入文件时没有清除列表,并且将新遇到的名称标记为“temp”。
do {
data = reader.readLine();
String nameEntity = null;
if (data != null) {
String[] splitText = data.split(",");
nameEntity = splitText[1];
}
if (temp != null) {
if (data == null || !(nameEntity.equals(temp))) {
if (tempList.size() >= 10) {
for (int i = 0; i < tempList.size(); i++) {
newCsvBW.append(tempList.get(i));
newCsvBW.append("\n");
System.out.println(tempList.get(i));
}
}
tempList.clear();
temp = nameEntity;
}
} else {
temp = nameEntity;
}
tempList.add(data);
} while (data != null);