Apache FileUtils class provides General file manipulation utilities, All the methods in this class that read a stream are buffered internally. This means that there is no cause to use a BufferedInputStream or BufferedReader.
Below written code will read String from .csv file, the file name will be provided as a argument in main method..
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
public class ReadingCSVFileDemo {
public static void main(String[] arg)throws IOException
{
List<String> existFileContent=new ArrayList<String>();
String newvalue=null;
FileReader reader = new FileReader(new File(arg[0]));
List<String> lines = IOUtils.readLines(reader);
try{
for (String value : lines) {
if(value!=null){
//newvalue=value.split(",");
existFileContent.add(value);
}
}System.out.println("size of List"+ existFileContent.size());
System.out.println("element in a list"+ existFileContent);
// below finally block we are closing a stream ignoring nulls and exceptions
}finally{IOUtils.closeQuietly(reader);}
}
}
0 Comment(s)