Java文件I / O问题

时间:2012-04-02 17:23:30

标签: java io

这是我第一次使用java中的文件i / o,但它无法正常工作。我解析单个行并输出分号分隔行的程序部分在我硬编码文件并在屏幕上显示时就像一个魅力。

当我尝试将文件public static OutputStream...写入illegal start to expression错误时,我无法让程序逐步浏览整个文件目录,而不是一时间

我不明白的地方:我注意在任何地方设置输出文件名...我应该这样做吗?路径变量不会通过。什么是路径的正确格式?任何人都可以在这里看到我需要调试的内容吗?

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());
       }

   }
}

3 个答案:

答案 0 :(得分:1)

Files.newOutputStream(c:, CREATE, APPEND);中,由于c:部分,您出现语法错误。您必须将Path个实例传递给Files.newOutputStream()

您可以使用Paths中的一种方法获取此类实例。

同样地,您似乎想要初始化startingPath这样的东西(使用字符串):

Path startingPath = "R:\Data\cs\RoboHelp\CorrLib\Output\Production\WebHelp";

但您需要使用PathsString转换为Path

Path startingPath = Paths.get("R:\Data\cs\RoboHelp\CorrLib\Output\Production\WebHelp");

答案 1 :(得分:1)

你的意思是:

Path startingDir = Paths.get("R:/Data/cs/RoboHelp/CorrLib/Output/Production/WebHelp");

同样在您的代码中,fstream未初始化。您是否在访问者的visitFile方法中复制了代码逻辑?

您在main方法中定义了一个方法 - 这是不允许的:

        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);
        }

答案 2 :(得分:0)

路径必须是双引号。