...
Code Block |
---|
language | java |
---|
title | TextFileReader |
---|
linenumbers | true |
---|
collapse | true |
---|
|
public class TextFileReader implements InputChannel
{
private String itsFileName;
private BufferedReader itsFileReader;
public TextFileReader()
{
}
public void openForWriting( String fname )
{
itsFileName = fname;
try
{
itsFileReader = new BufferedReader(new FileReader(fname));
} catch (IOException ex)
{
Logger.getLogger(TextFileWriter.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public String read()
{
StringBuilder sb = new StringBuilder();
String line;
try
{
while( (line = itsFileReader.readLine()) != null )
sb.append(line );
} catch (IOException ex)
{
Logger.getLogger(TextFileReader.class.getName()).log(Level.SEVERE, null, ex);
}
finally
{
close();
}
return sb.toString();
}
@Override
public void close()
{
try
{
itsFileReader.close();
} catch (IOException ex)
{
Logger.getLogger(TextFileReader.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
|
Now add the following lines of code to MainUnit to test your code
Code Block |
---|
language | java |
---|
title | MainUnit - reading from a file |
---|
linenumbers | true |
---|
collapse | true |
---|
|
// Read the files back in
System.out.println( "===========================================" );
TextFileReader tfr = new TextFileReader();
tfr.openForWriting("c:\\tmp\\account.json");
String jsonStream = tfr.read();
System.out.println( jsonStream );
tfr.openForWriting("c:\\tmp\\account.xml");
String xmlStream = tfr.read();
System.out.println( xmlStream );
tfr.openForWriting("c:\\tmp\\customer.json");
jsonStream = tfr.read();
System.out.println( jsonStream ); |
Related articles
Filter by label (Content by label) |
---|
showLabels | false |
---|
max | 5 |
---|
spaces | com.atlassian.confluence.content.render.xhtml.model.resource.identifiers.SpaceResourceIdentifier@2007c5 |
---|
showSpace | false |
---|
sort | modified |
---|
reverse | true |
---|
type | page |
---|
cql | label = "kb-troubleshooting-article" and type = "page" and space = "DJFE" |
---|
labels | kb-troubleshooting-article |
---|
|
...