我已将一组值从csv文件导入到数组列表中。现在我需要从每个元素中删除扩展名.tar,或者在从csv文件导入时只允许前8个字符插入到数组列表中。这是我的代码,我想在array1部分
中进行更改import java.io.*;
import java.util.*;
import java.util.ArrayList;
import java.lang.*;
public class compare
{
public static void main(String[] args)
{
ArrayList<String> array = new ArrayList<String>(); //Array for storing values
ArrayList<String> array1 = new ArrayList<String>(); //Array for storing values
try
{
String strFile = "D:\\Ramakanth\\PT2573\\ftp.csv"; //csv file containing data
BufferedReader br = new BufferedReader( new FileReader(strFile)); //create BufferedReader to
String strLine = "";
StringTokenizer st = null;
while( (strLine = br.readLine()) != null) //read comma separated file line by line
{
st = new StringTokenizer(strLine, ","); //break comma separated line using ","
while(st.hasMoreTokens())
{
array.add(st.nextToken()); //store csv values in array
}
}
}
catch(Exception e)
{
System.out.println("Exception while reading csv file: " + e);
}
try
{
String strFile1 = "D:\\Ramakanth\\PT2573\\target.csv"; //csv file containing data
BufferedReader br1 = new BufferedReader( new FileReader(strFile1)); //create BufferedReader
String strLine1 = "";
StringTokenizer st1 = null;
while( (strLine1 = br1.readLine()) != null) //read comma separated file line by line
{
st1 = new StringTokenizer(strLine1, ","); //break comma separated line using ","
while(st1.hasMoreTokens())
{
array1.add(st1.nextToken()); //store csv values in array
}
}
}
catch(Exception e)
{
System.out.println("Exception while reading csv file: " + e);
}
array.removeAll(array1);
System.out.println(array);
try
{
BufferedWriter br2 = new BufferedWriter(new FileWriter("D:\\Ramakanth\\PT2573\\output.csv"));
StringBuilder sb1 = new StringBuilder();
for (String element : array)
{
sb1.append(element);
sb1.append(" ");
}
br2.write(sb1.toString());
br2.close();
}
catch(Exception e)
{
System.out.println("Exception while writing csv file: " + e);
}
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
试试这个,
String element = "extension.tar";
int index = element.lastIndexOf(".");
System.out.println("if Dot extension means : " + element.substring(0, index));
element = element.substring(0, 8);
System.out.println("if first 8 character means : " + element);
<强>输出:强>
如果Dot扩展意味着:扩展名
如果前8个字符表示:extensio