spring - java temporary files created but contents write issue -
package com.studytrails.tutorials.springremotingrmiserver; import java.lang.object; import java.awt.desktop; import java.io.*; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import org.springframework.core.io.resource; public class greetingserviceimpl implements greetingservice { @override public string getgreeting(string name) { return "hello " + name + "!"; } public string gettext() { applicationcontext appcontext = new classpathxmlapplicationcontext(new string[]{"spring-config-server.xml"}); resource resource = appcontext.getresource("file:d:\\text\\test.txt"); stringbuilder builder = new stringbuilder(); try{ inputstream = resource.getinputstream(); bufferedreader br = new bufferedreader(new inputstreamreader(is)); file temp=file.createtempfile("output", ".tmp"); string filepath=temp.getabsolutepath(); system.out.println(""+filepath); string line; printwriter out = new printwriter(new filewriter(temp)); //system.out.println(""+filepath); while ((line = br.readline()) != null) { out.println(line); } string tem=temp.getname(); //temp.setreadonly(); string[] cmd = {"notepad",tem}; runtime runtime = runtime.getruntime(); process proc = runtime.exec(cmd); out.close(); br.close(); temp.deleteonexit(); }catch(ioexception e){ e.printstacktrace(); } return builder.tostring(); } }
in above code creates temporary file trying write contents in file when program executed notepad open temporary name gives message file not exists want create. need temporary file text present in d:\text\test.txt location. kindly suggest me
ok here updated code should work. have writen java program working. main problem calling temp.deleteonexit();
after process open. have wait process complete, other wise main thread delete file before notepad opens it. use out.write(line);
hope helps.
string line; printwriter out = new printwriter(new filewriter(temp)); while ((line = br.readline()) != null) { out.write(line); } out.flush(); out.close(); br.close(); string[] cmd = { "notepad", filepath }; runtime runtime = runtime.getruntime(); process proc = runtime.exec(cmd); out.close(); br.close(); try { proc.waitfor(); } catch (interruptedexception e) { e.printstacktrace(); } temp.deleteonexit();
Comments
Post a Comment