简体   繁体   中英

How to write and read json or txt files spring boot?

I'm trying to create a simple manual Caching system in my server by using Spring Boot. To succeed it, I wrote two different files, one of it for caching time and other one is the data. I planned to write at first request and other requests in 5 minutes will call from file.

The problem is the flow working correctly in my localhost but when I deploy my code, spring boot can not create a new file and read it of course.

The codes are my writing and reading like below.

 @Override
public void writeFile( String folder, String path, String data ) throws IOException {
    File file = new File( folder );
    if ( !file.exists() ) {
        if ( file.mkdir() ) {
            System.out.println( "Directory is created!" );
        } else {
            System.out.println( "Failed to create directory!" );
        }
    }

    BufferedWriter writer = new BufferedWriter( new FileWriter( path ) );
    writer.write( data );

    writer.flush();
    writer.close();
}

@Override
public String readFile( File file ) {
    BufferedReader reader = null;
    StringBuilder buffer = new StringBuilder();

    try {
        reader = new BufferedReader( new FileReader( file ) );
        String text;

        while ( ( text = reader.readLine() ) != null ) {
            buffer.append( text );
        }
    } catch ( IOException e ) {
        e.printStackTrace();
    } finally {
        try {
            if ( reader != null ) {
                reader.close();
            }
        } catch ( IOException e ) {
            e.printStackTrace();
        }
    }

    return buffer.toString();
}

When I call write file method in Localhost, it is creating the necessary files and folders. In the picture below, it is creating lastsavedtime.txt and readerlist.json.

方法创建的文件

And readFile method reads the files successfully. The same process not working on server.

Spring Boot version: 2.0.5.RELEASE Azure Web App as server.

Thank you for helping.

Supplementary notes about the comment.

@RequestMapping(value="/newFile", method=RequestMethod.GET)
    public String newFile(String folder, String path, String data){     
//      File file = new File("D:/home/site/wwwroot/testfile");      
        File file = new File( folder );
        file.mkdirs();
        try {
//          BufferedWriter writer = new BufferedWriter( new FileWriter("D:/home/site/wwwroot/testfile/cout.txt") );
            BufferedWriter writer = new BufferedWriter( new FileWriter( path ) );    
            writer.write( data );       
            writer.flush();
            writer.close();   
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return "hello world";
    }

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM