Java 无法修改REST Web服务中的文本文件

Java 无法修改REST Web服务中的文本文件,java,rest,Java,Rest,我的程序实现了RESTWeb服务的方法X,该服务调用另一个类中的另一个方法Y。后者修改文本文件中的一行 我在没有使用REST方法X的情况下测试了方法Y,一切都很顺利,文件也被更改了。但是当我调用PUT方法X时,文件没有改变 web服务的Java方法如下所示: public String reduceEnergyConsumption(int id, String action) { String ch; int nb; if (action=="r

我的程序实现了RESTWeb服务的方法X,该服务调用另一个类中的另一个方法Y。后者修改文本文件中的一行

我在没有使用REST方法X的情况下测试了方法Y,一切都很顺利,文件也被更改了。但是当我调用PUT方法X时,文件没有改变

web服务的Java方法如下所示:

public String reduceEnergyConsumption(int id, String action) {
        String ch;
        int nb;
        if (action=="reduce")
            AQSensor.setState(id,"Off"); 
        else
            AQSensor.setState(id,"On");

        return action;
}
public void setState(int id, String state) {
        Vector<String> VInter= new Vector<String>();
        try {
            InputStream ips = new FileInputStream("stateAQS.txt");
            InputStreamReader ipsr = new InputStreamReader(ips);
            BufferedReader br = new BufferedReader(ipsr);
            String ligne;
            int i=0;
            while ((ligne = br.readLine()) != null)
            {
                i++;   
                VInter.add(ligne);
            }
            VInter.setElementAt(state, id);
    //****move the vector in the file
            OutputStream ops = new FileOutputStream("stateAQS.txt");
            OutputStreamWriter opsw = new OutputStreamWriter(ops);
            BufferedWriter bw = new BufferedWriter(opsw);
            for(int e=0;e<VInter.size();e++){
                bw.append(VInter.elementAt(e));
                bw.newLine();
            }
            bw.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Scanner sc = new Scanner(System.in);
System.out.println("enter the action: ");
String action = sc.nextLine();
System.out.println("enter the index: ");
String pr= sc.nextLine();

Entity<String> userEntity = Entity.entity(action, MediaType.TEXT_PLAIN);
Response response = target.path("aqsensor").path("reduceEnergy/"+pr+"/"+action).request().put(userEntity);

System.out.println("response is: "+response.getStatus());
AQ传感器是:

static AirQualitySensorManager AQSensor= new AirQualitySensorManagerImp();
setState(id,state)方法如下:

public String reduceEnergyConsumption(int id, String action) {
        String ch;
        int nb;
        if (action=="reduce")
            AQSensor.setState(id,"Off"); 
        else
            AQSensor.setState(id,"On");

        return action;
}
public void setState(int id, String state) {
        Vector<String> VInter= new Vector<String>();
        try {
            InputStream ips = new FileInputStream("stateAQS.txt");
            InputStreamReader ipsr = new InputStreamReader(ips);
            BufferedReader br = new BufferedReader(ipsr);
            String ligne;
            int i=0;
            while ((ligne = br.readLine()) != null)
            {
                i++;   
                VInter.add(ligne);
            }
            VInter.setElementAt(state, id);
    //****move the vector in the file
            OutputStream ops = new FileOutputStream("stateAQS.txt");
            OutputStreamWriter opsw = new OutputStreamWriter(ops);
            BufferedWriter bw = new BufferedWriter(opsw);
            for(int e=0;e<VInter.size();e++){
                bw.append(VInter.elementAt(e));
                bw.newLine();
            }
            bw.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Scanner sc = new Scanner(System.in);
System.out.println("enter the action: ");
String action = sc.nextLine();
System.out.println("enter the index: ");
String pr= sc.nextLine();

Entity<String> userEntity = Entity.entity(action, MediaType.TEXT_PLAIN);
Response response = target.path("aqsensor").path("reduceEnergy/"+pr+"/"+action).request().put(userEntity);

System.out.println("response is: "+response.getStatus());
那好吧。但文件行没有更改。问题在哪里


提前感谢。

问题是您没有指定完整的文件路径。由于只指定文件名,应用程序将在当前目录中搜索该文件。调用RESTAPI方法时,当前目录不是文件所在的目录,并且该方法无法找到该文件


您可以通过将完整路径名指定为
FileInputStream
FileOutputStream

的参数来解决问题。是否在修改流结束时关闭所有流?是的,我关闭了。请尝试捕获所有异常,而不仅仅是IOExceptions,然后查看发生了什么。请检查网络流量日志,看看是否一切正常。请使用调试器或将行打印到日志文件中,逐步检查服务器代码,并检查是否将正确的行传递到bw.append()。如果捕获所有异常,它将显示如下:java.io.FileNotFoundException:stateAQS.txt(Le fichier spécifiéest introvable)另外,请确保您的文件是相对于ressource文件夹的。最好是将ressource获取为当前目录,然后使用getRessource(“.”.getpath添加您的相对ressource路径