从Eclipse Project中的文件中读取

时间:2014-03-10 01:03:03

标签: java xml eclipse

我想创建一个应用程序,将inputfolder内的大文本文件拆分为几个小文件放在outputfolder内。

这是项目大纲:

enter image description here

以下代码在从外部文件夹获取文件时工作正常,但是当我将其修改为从项目内部的文件夹中读取时,它给了我这个错误:

Exception in thread "main" java.lang.NullPointerException
at com.zakaria.cut.XmlCutter.cut(XmlCutter.java:45)
at com.zakaria.cut.Main.main(Main.java:8)

[XmlCutter.java]

package com.zakaria.cut;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.MessageFormat;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class XmlCutter {

 private static final String OUTPUT_FILE_NAME = "/file";
 //private static String USER_HOME = System.getProperty("user.home");
 private static final String INPUT_FOLDER = "../inputfolder";
 private static String OUTPUT_FOLDER = "../outputfolder";
 private static Logger LOG = Logger.getLogger("XmlCutter");
 private static long COUNTER = 0;

 public XmlCutter() {
super();
// TODO Auto-generated constructor stub
}

public void cut() {
  Handler h = new ConsoleHandler();
  h.setLevel(Level.FINE);
  LOG.addHandler(h);
  LOG.setLevel(Level.FINE);

  File inputDir = new File(INPUT_FOLDER);
  File[] filesInInputDir = inputDir.listFiles();
  for (File f : filesInInputDir) {
   if ((f.getName()).endsWith(".txt")) {
    LOG.fine((MessageFormat.format(
      "Found a text file {0}. Processing docs...",
      f.getName())));
    processFile(f);
   }
  }
 }

 private static void processFile(File f) {
  StringBuilder out = new StringBuilder();
  char prev = '#';
  try {
   BufferedReader br = new BufferedReader(new InputStreamReader(
     new FileInputStream(f), "UTF8"));
   char[] buf = new char[1];
   while (br.read(buf) >= 0) {
    out.append(buf[0]);
    if (prev == '<' && buf[0] == '?') {
     LOG.finest((MessageFormat.format(
       "Start of XML PI Found: {0}{1}", prev, buf[0])));
     if (out.length() > 2) {
      flushToFile(out.substring(0, out.length() - 2));
     }
     out.setLength(2);
    }
    prev = buf[0];
   }
   LOG.finest("Writing final file");
   flushToFile(out.toString());
   br.close();
  } catch (IOException e) {
   LOG.fine(e.getMessage());
  }
  LOG.fine(MessageFormat.format("Generated {0} XML Documents", COUNTER));
 }

 private static void flushToFile(String s) {
  File f = new File(OUTPUT_FOLDER + OUTPUT_FILE_NAME + (++COUNTER)
    + ".xml");
  LOG.finest(MessageFormat.format("Writing file: {0}", f.getName()));
  try {
   FileOutputStream fos = new FileOutputStream(f);
   OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
   osw.write(s);
   osw.flush();
  } catch (IOException e) {
   LOG.fine(e.getMessage());
  }
 }
}

[Main.java]

package com.zakaria.cut;

public class Main {
    public static void main(String[] args) {
        XmlCutter cutter = new XmlCutter();
        cutter.cut();
    }
}

问题,我想,肯定是在这里:

 private static final String INPUT_FOLDER = "../inputfolder";
 private static String OUTPUT_FOLDER = "../outputfolder";

我该如何解决?

1 个答案:

答案 0 :(得分:1)

你知道程序正在执行什么文件夹吗?我的猜测是相对链接指向错误的位置?您是否尝试过对路径进行硬编码并查看它们是否有效?如果他们这样做,您可能需要查看执行文件夹,然后相应地更改相对路径?