我不能在java中将文本文件设置为只读模式

我不能在java中将文本文件设置为只读模式,java,file-io,Java,File Io,在上面的代码中,我无法设置readonly();命令到临时文件。显示所有选项的文件。您可以建议如何使临时文件不被修改,甚至无法保存到其他位置。我需要这个文件只显示在运行时的程序。在此期间,用户不会更改任何内容,也无法将其另存为其他位置。我建议在使用该文件之前关闭该文件(通过记事本): 为了避免用户将文件移动到另一个位置,您可以在临时文件夹中创建临时文件,并将文件夹设置为只读。但如果用户可以读取,他们通常可以将文件复制到其他位置。如果我记得很清楚,在Windows中,您可以标记为读取,但不能复制文

在上面的代码中,我无法设置readonly();命令到临时文件。显示所有选项的文件。您可以建议如何使临时文件不被修改,甚至无法保存到其他位置。我需要这个文件只显示在运行时的程序。在此期间,用户不会更改任何内容,也无法将其另存为其他位置。

我建议在使用该文件之前关闭该文件(通过记事本):

为了避免用户将文件移动到另一个位置,您可以在临时文件夹中创建临时文件,并将文件夹设置为只读。但如果用户可以读取,他们通常可以将文件复制到其他位置。如果我记得很清楚,在Windows中,您可以标记为读取,但不能复制文件(当然是通过Windows)

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