This is my first time working with file i/o in java, and it's not working. The section of the program where I parse individual lines and output a semicolon delimited line works like a charm when I hardcode a file and display on screen.
Whne I try to write to a file public static OutputStream... errors out as an illegal start to expression, and I've been unable to get the program to step through an entire directory of files instead of one at a time.
Where I'm not clear: I'm note setting an output filename anywhere...whare am I supposed to do that? The path variable won't pass. What's the proper format for a path? Can anyone see what I need to debug here?
import java.io.*;
public class FileRead
{
public static void main(String args[])
{
try
{
// Open the file(s)
// single file works OK FileInputStream fstream = new FileInputStream("testfile.txt");
Path startingDir = R:\Data\cs\RoboHelp\CorrLib\Output\Production\WebHelp;
PrintFiles pf = new PrintFiles();
Files.walkFileTree(startingDir, pf);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String inputLine;
String desc = "";
String docNo = "";
String replLtr = "";
String specCond = "";
String states = "";
String howGen = "";
String whenGen = "";
String owner = "";
String lastChange = "";
//Read File Line By Line
while ((inputLine = br.readLine()) != null)
{
int testVal=0;
int stringMax = inputLine.length();
//
if(inputLine.startsWith("Description"))
{desc = inputLine.substring(13,inputLine.length());}
else if(inputLine.startsWith("Reference Number"))
{docNo = inputLine.substring(20,inputLine.length());}
else if(inputLine.startsWith("Replaces Letter"))
{replLtr = inputLine.substring(17,inputLine.length());}
else if(inputLine.startsWith("Special Conditions"))
{specCond = inputLine.substring(21,inputLine.length());}
else if(inputLine.startsWith("States Applicable"))
{states = inputLine.substring(19,inputLine.length());}
else if(inputLine.startsWith("How Generated"))
{howGen = inputLine.substring(15,inputLine.length());}
else if(inputLine.startsWith("When Generated"))
{whenGen = inputLine.substring(16,inputLine.length());}
else if(inputLine.startsWith("Owner"))
{owner = inputLine.substring(7,inputLine.length());}
else if(inputLine.startsWith("Last Change Date"))
{lastChange = inputLine.substring(17,inputLine.length());}
}
//close while loop
// Print the content on the console
String outStr1 = (desc + ";" + docNo + ";" + replLtr + ";" + specCond + ";" + states);
String outStr2 = (";" + howGen + ";" + whenGen + ";" + owner + ";" + lastChange);
String outString = (outStr1 + outStr2);
System.out.print(inputLine + "\n" + outString);
String lineItem = (outStr1+outStr2);
//
try (OutputStream out = new BufferedOutputStream
(logfile.newOutputStream(CREATE, APPEND)))
{
out.write(lineItem, 0, lineItem.length);
}
catch (IOException x)
{
System.err.println(x);
}
public static OutputStream newOutputStream() throws IOException
{
// append to an existing file, create file if it doesn't initially exist
out = Files.newOutputStream(c:, CREATE, APPEND);
}
//Close the input stream
in.close();
}
catch (Exception e)
{
//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}