Reputation: 1289
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 is = resource.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
File temp=File.createTempFile("output", ".tmp");
String filePath=temp.getAbsolutePath();
System.out.println(""+filePath);
String tem=temp.getName();
String line;
PrintWriter out = new PrintWriter(new FileWriter(tem));
//System.out.println(""+filePath);
while ((line = br.readLine()) != null) {
out.println(line);
}
temp.setReadOnly();
String[] cmd = {"notepad",tem};
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);
// proc.getInputStream();
out.close();
br.close();
//temp.deleteOnExit();
}
catch(IOException e) {
e.printStackTrace();
}
return builder.toString();
}
}
In above code I am not able to setReadonly(); command to temp file. File displayed with all options can you suggest how to make the temp file as not modified and even it is not able to save to another location. I need this file only displayed at run time of the program. During that time user does not change any content and it could not be save as another location.
Upvotes: 1
Views: 822
Reputation: 24630
I suggest to close the file before using it (by notepad):
PrintWriter out = new PrintWriter(new FileWriter(tem));
//System.out.println(""+filePath);
while ((line = br.readLine()) != null) {
out.println(line);
}
out.close();
temp.setReadOnly();
String[] cmd = {"notepad",tem};
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);
// proc.getInputStream();
To avoid that a user move the file to another location you may additional create the temp file in a temp folder and make the folder readonly too. But if user can read they normally can copy the file to another location. If I remember well, in Windows you can mark for read but prevent from copy a file (thru Windows of course).
Upvotes: 1