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.nio.*;
public class FileReadSSCCE
{
public static void main(String args[])
{
try
{
Path startingDir = Paths.get("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 = "";
//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());}
}
// Print the content on the console
String outStr1 = (desc + ";" + docNo);
System.out.print(inputLine + "\n" + outStr1);
String lineItem = (outStr1);
//
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((Paths.get("c:\javaout.txt"), CREATE, APPEND);
}
//Close the input stream
in.close();
}
catch (Exception e)
{
//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
Do you mean:
?
Also in your code, fstream is not initialised. Have you copied your code logic in the
visitFilemethod of your Visitor?And you define a method within your main method – which is not allowed: