开发者

Write to text file in Java WebService

For school I have to make a webservice as a part of a exercise. In this program I must use a .txt file which will serve as a database. When I use a BufferedWriter it says that the information is succesfully written to the file. But the file is still unchanged. Reading from the file went well. Thanks in advance!

The code:

    @WebService
public class Vak {

@WebMethod
    public boolean addLesson(String lessonname, double mark){       
        if(!lessonname.equals("") || !(mark == 0.00)){
            try{
                FileInputStream fis = new FileInputStream("C:/marks.txt");
                DataInputStream in = new DataInputStream(fis);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;         
                while ((strLine = br.readLine()) != null){
                    String [] splitted = strLine.split(" ");
                    if(splitted[0].equalsIgnoreCase(lessonname)){
                        System.out.println("Lesson already exists");
                        return false;
                    }
            开发者_开发百科    }           
                BufferedWriter out = new BufferedWriter(new FileWriter("C:/marks.txt", true));
                out.newLine();
                out.write(lessonname + " " + mark);
                return true;
            }
            catch(Exception e){
                System.out.println("Exception " + e);
                return false;
            }
        }   
        return false;
    }
}


Add

out.close();

before you return true;

From the documentation, the close method does the following:

Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜