Skip to content

Instantly share code, notes, and snippets.

@lnsp
Created September 13, 2015 16:18
Show Gist options
  • Select an option

  • Save lnsp/ca92e222992590c1aeec to your computer and use it in GitHub Desktop.

Select an option

Save lnsp/ca92e222992590c1aeec to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class FileReaderDemo
{
private BufferedReader reader;
public void open(String path)
{
try
{
FileReader finReader = new FileReader(path);
reader = new BufferedReader(finReader);
}
catch (IOException e)
{
e.printStackTrace();
reader = null;
}
}
public String readLine()
{
if (reader == null)
return null;
try
{
return reader.readLine();
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
public String[] readTokens(String pattern)
{
if (reader == null)
return null;
String line;
try
{
line = reader.readLine();
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
if (line == null)
return null;
return line.split(pattern);
}
public void close()
{
try
{
reader.close();
reader = null;
}
catch (IOException e)
{
e.printStackTrace();
}
}
public boolean isOpen()
{
return reader != null;
}
public static void main(String[] args) {
FileReaderDemo demo = new FileReaderDemo();
demo.open("demo.txt");
String[] tokens;
while ((tokens = demo.readTokens(";")) != null)
{
System.out.print("Zeile:");
for (int i = 0; i < tokens.length; i++)
{
System.out.print(" - " + tokens[i]);
}
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment