Parsing file path in java

Code Comments

The following code is designed to parse (comma, tab, etc.) delimited files in Java.

private static ArrayList parseDelimitedFile(String filePath, String delimiter) throws Exception
ArrayList rows = new ArrayList();

FileReader fr = new FileReader(filePath);
BufferedReader br = new BufferedReader(fr);

String currentRecord;
while((currentRecord = br.readLine()) != null)
rows.add(currentRecord.split(delimiter));

Share this:

Like this:

18 Comments Add your own

Will this help to parse a huge text file ( about 500 Mb ). Please help me on this. Actually I need to parse from a big file to a table. Also I need to do some data manipulation while parsing the text files lines fileds with exisiting table. What would be the fastest way to do this. Thanks

This should work to parse large files. I have used similar code to process multi-GB files. What you’d have to change, though, is that this code stores the results in an ArrayList object, so if you are processing huge files it will consume a lot of memory. But if you are pulling the data from a file and then storing it in a database immedidately, you should not have to worry about memory. If you’re wanting some code samples on how to manipulate the text after pulling from the file and before inserting into the database, please send me more details, and I’ll see what I can cook up.

If you want more flexibility and validation functionality you could consider using the open source library http://jsapar.tigris.org. It uses an xml based schema to describe the structure of the file to parse.
You can register an event listener for each line that is parsed, thus it does not require to load more than one line at a time into the memory.

Читайте также:  Bitrix settings php https

Hi, I am having one data file which contains the below format [HEADER: BUSINESS DATE=20110914] [RESTDAYS]
AED=FRI
AFN=FRI:SAT II want to get the currency asndays alone from the above file and store it to arraylist.. Please help me out…

Hi, I am having one data file which contains the below format [HEADER: BUSINESS DATE=20110914] [RESTDAYS]
AED=FRI
AFN=FRI:SAT II want to get the currency asndays alone from the above file and store it to arraylist.. Please help me out…

import java.io.*;
import java.util.*;
public class ParsingFileProp < Properties p=new Properties(); public static void parseDelimitedFile( String filePath1, String outPath1) throws Exception
FileWriter f=new FileWriter(outPath1);
BufferedReader br = new BufferedReader(new FileReader(filePath1));
String currentRecord=null;
List records =new ArrayList();
while((currentRecord = br.readLine()) != null)
records.add(currentRecord);
>
Iterator itr = records.iterator();
while(itr.hasNext())
String element = (String) itr.next();
if(element.startsWith(“[“))
continue;
else if(element.contains(“=”))
String result = java.util.Arrays.toString(element.split(“\\=”));
String currency=result.substring(1,4);
f.append(“CURRENCY ———->”+currency+”\n”);
int len = result.length();
int lastindex=len-1;
String day = result.substring(6,lastindex);
f.append(“DAYS————–>”+day+”\n”);
>
f.flush();
>
> public static void main(String args[])throws Exception
Properties p=new Properties();
try
p.load(new FileInputStream(“file1.properties”));
>
catch(FileNotFoundException f)
System.out.println(“DataFile is not found in properties file”+f);
>
String datFile = p.getProperty(“filePath”);
String outFile = p.getProperty(“outPath”);
ParsingFileProp p1=new ParsingFileProp();
p1.parseDelimitedFile(datFile,outFile); >

Type safety: The method add(Object) belongs to the raw type ArrayList. References to generic
type ArrayList should be parameterized

The above error will be fixed. if we parameterize the arraylist as string type like ArrayList list = new ArrayList (); Though I got the error ” The method add(String) in the type ArrayList is not applicable for the arguments (String[]) “

The above error will be fixed. if we parameterize the arraylist as string type like ArrayList list = new ArrayList (); Though I got the error ” The method add(String) in the type ArrayList is not applicable for the arguments (String[]) “

plz help me,i need “Read the file seperated by comma and following requirement
1)considerble words are alphabet or combination of alphabet and numerics or nuemerics
ex:rama,
hari234
raja-453.432
rahim-872 etc

Aw, this was a very nice post. Taking the time
and actual effort to generate a very good article… but what can I
say… I hesitate a whole lot and don’t seem to get nearly anything done.

Hi there, just became alert to your blog through Google, and found that it’s really informative. I am going to watch out for brussels. I’ll appreciate if
you continue this in future. Numerous people will be benefited from
your writing. Cheers!

Opportunities- These are environmental factors that can be exploited.
The competitor generates first party information directly.
Owning business premises in a SMSF can make a great deal of sense for SMSF’s and business owners alike.

Источник

Parsing a file with Stream API in Java 8

Streams are everywhere in Java 8. Just look around and for sure you will find them. It also applies to java.io.BufferedReader . Parsing a file in Java 8 with Stream API is extremely easy. I have a CSV file that I want to be read. An example below:

username;visited jdoe;10 kolorobot;4
  • Open a source for reading,
  • Get the first line and parse it,
  • Split line by a separator,
  • Get the first line and parse it,
  • Convert the line to list of strings and return.
class CsvReader < private static final String SEPARATOR = ";"; private final Reader source; CsvReader(Reader source) < this(source); >List readHeader() < try (BufferedReader reader = new BufferedReader(source)) < return reader.lines() .findFirst() .map(line ->Arrays.asList(line.split(SEPARATOR))) .get(); > catch (IOException e) < throw new UncheckedIOException(e); >> >

Fairly simple. Self-explanatory. Similarly, I created a method to read all records. The algorithm for reading the records is as follows:

  • Open a source for reading,
  • Skip the first line,
  • Split line by a separator,
  • Apply a mapper on each line that maps a line to a list of strings.
class CsvReader < List> readRecords() < try (BufferedReader reader = new BufferedReader(source)) < return reader.lines() .substream(1) .map(line ->Arrays.asList(line.split(separator))) .collect(Collectors.toList()); > catch (IOException e) < throw new UncheckedIOException(e); >> >

Nothing fancy here. What you could notice is that a mapper in both methods is exactly the same. In fact, it can be easily extracted to a variable:

Function> mapper = line -> Arrays.asList(line.split(separator));

To finish up, I created a simple test.

public class CsvReaderTest < @Test public void readsHeader() < CsvReader csvReader = createCsvReader(); Listheader = csvReader.readHeader(); assertThat(header) .contains("username") .contains("visited") .hasSize(2); > @Test public void readsRecords() < CsvReader csvReader = createCsvReader(); List records = csvReader.readRecords(); assertThat(records) .contains(Arrays.asList("jdoe", "10")) .contains(Arrays.asList("kolorobot", "4")) .hasSize(2); > private CsvReader createCsvReader() < try < Path path = Paths.get("src/test/resources", "sample.csv"); Reader reader = Files.newBufferedReader( path, Charset.forName("UTF-8")); return new CsvReader(reader); >catch (IOException e) < throw new UncheckedIOException(e); >> >

Источник

blog.codeleak.pl

Streams are everywhere in Java 8. Just look around and for sure you will find them. It also applies to java.io.BufferedReader . Parsing a file in Java 8 with Stream API is extremely easy.

I have a CSV file that I want to be read. An example below:

username;visited jdoe;10 kolorobot;4

A contract for my reader is to provide a header as list of strings and all records as list of lists of strings. My reader accepts java.io.Reader as a source to read from.

  • Open a source for reading,
  • Get the first line and parse it,
  • Split line by a separator,
  • Get the first line and parse it,
  • Convert the line to list of strings and return.
class CsvReader < private static final String SEPARATOR = ";"; private final Reader source; CsvReader(Reader source) < this.source = source; >List readHeader() < try (BufferedReader reader = new BufferedReader(source)) < return reader.lines() .findFirst() .map(line ->Arrays.asList(line.split(SEPARATOR))) .get(); > catch (IOException e) < throw new UncheckedIOException(e); >> >
  • Open a source for reading,
  • Skip the first line,
  • Split line by a separator,
  • Apply a mapper on each line that maps a line to a list of strings
class CsvReader < List> readRecords() < try (BufferedReader reader = new BufferedReader(source)) < return reader.lines() .substream(1) .map(line ->Arrays.asList(line.split(separator))) .collect(Collectors.toList()); > catch (IOException e) < throw new UncheckedIOException(e); >> >

Nothing fancy here. What you could notice that a mapper in both methods is exactly the same. In fact, it can be easily extracted to a variable:

Function> mapper = line -> Arrays.asList(line.split(separator));
public class CsvReaderTest < @Test public void readsHeader() < CsvReader csvReader = createCsvReader(); Listheader = csvReader.readHeader(); assertThat(header) .contains("username") .contains("visited") .hasSize(2); > @Test public void readsRecords() < CsvReader csvReader = createCsvReader(); List records = csvReader.readRecords(); assertThat(records) .contains(Arrays.asList("jdoe", "10")) .contains(Arrays.asList("kolorobot", "4")) .hasSize(2); > private CsvReader createCsvReader() < try < Path path = Paths.get("src/test/resources", "sample.csv"); Reader reader = Files.newBufferedReader( path, Charset.forName("UTF-8")); return new CsvReader(reader); >catch (IOException e) < throw new UncheckedIOException(e); >> >

Источник

Parsing file path in java

Path testFile = Paths.get(«C:\\Users\\jleom\\Desktop\\java\\javarush task\\test.txt»); Path testFile2 = Paths.get(«C:\\Users\\jleom\\Desktop»); System.out.println(testFile.relativize(testFile2));

Класс Path и класс Paths предназначены для работы с файловой системой в Java, однако они предоставляют разные функции и методы. Path — это интерфейс, который определяет методы для работы с путями к файлам и каталогам в файловой системе. Он предоставляет ряд методов для работы с путями, таких как resolve(), relativize(), getParent(), getFileName(), toAbsolutePath() и другие. Paths — это утилитный класс, который предоставляет статические методы для создания экземпляров класса Path. Он не имеет методов для работы с путями напрямую, но предоставляет методы для создания экземпляров Path из строковых значений или URI. Еще методы по классу Paths: getFileSystem(): возвращает объект FileSystem, представляющий файловую систему, которой принадлежит данный путь. getDefault(): возвращает объект FileSystem, представляющий файловую систему по умолчанию. getTempDirectory(): возвращает объект типа Path, представляющий временный каталог. getHomeDirectory(): возвращает объект типа Path, представляющий домашний каталог пользователя. exists(Path path, LinkOption. options): проверяет, существует ли файл или каталог, представленный указанным путем. Класс Paths удобен для работы с файловой системой, так как он предоставляет простой и удобный API для работы с путями.

Надо добавить в статью, Paths.get был в 8 Java. Потом появился Path.of. Если у вас не работает Path.of (версия Java не позволяет), только тогда нужен Paths.get

Источник

Оцените статью