java - Read from file inside Eclipse Project -
i want make application splits big text file inside inputfolder
several small xml files put inside outputfolder
.
this project outline:
the following code works fine when comes getting file outside folder, when modified read folder inside project, gave me error:
exception in thread "main" java.lang.nullpointerexception @ com.zakaria.cut.xmlcutter.cut(xmlcutter.java:45) @ 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(); (file f : filesininputdir) { if ((f.getname()).endswith(".txt")) { log.fine((messageformat.format( "found 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(); } }
the problem, guess, here:
private static final string input_folder = "../inputfolder"; private static string output_folder = "../outputfolder";
how can fix it?
do know folder program executing from? guess relative links pointing wrong spot? have tried hard coding paths , see if work? if might have @ execution folder , change relative paths accordingly?
Comments
Post a Comment