My question is this:
How can you write to a .txt file (or excel, doesn't really matter) in java and read from it to compare it to other variables.
Any Ideas or examples?
Thanks !@
Printable View
My question is this:
How can you write to a .txt file (or excel, doesn't really matter) in java and read from it to compare it to other variables.
Any Ideas or examples?
Thanks !@
Welcome to the forums
Hope this code helps
Code:import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class FileReader
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
DataInputStream reader = null;
if ( args.length < 1 )
{
System.out.println("Usage: FileReader path");
return;
}
System.out.print(readTextFile(args[0]));
}
/**
* Read from a file into a string
* @param filePath
* @return
*/
private static String readTextFile(String filePath)
{
BufferedReader reader = null;
try
{
reader = new BufferedReader(new java.io.FileReader(filePath));
StringBuilder builder = new StringBuilder();
while (reader.ready())
{
builder.append(reader.readLine() + "\n");
}
return builder.toString();
}
catch (FileNotFoundException ex)
{
Logger.getLogger(FileReader.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex)
{
Logger.getLogger(FileReader.class.getName()).log(Level.SEVERE, null, ex);
}
finally
{
try
{
reader.close();
}
catch (IOException ex)
{
Logger.getLogger(FileReader.class.getName()).log(Level.SEVERE, null, ex);
}
}
return null;
}
}