简体   繁体   English

如何读写json或txt文件spring引导?

[英]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.我正在尝试使用 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.我计划在第一个请求时写,5 分钟内的其他请求将从文件中调用。

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.问题是流程在我的本地主机上正常工作,但是当我部署我的代码时,spring 引导无法创建新文件并当然无法读取它。

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.当我在 Localhost 中调用写文件方法时,它正在创建必要的文件和文件夹。 In the picture below, it is creating lastsavedtime.txt and readerlist.json.在下图中,它正在创建 lastsavedtime.txt 和 readerlist.json。

方法创建的文件

And readFile method reads the files successfully.并且 readFile 方法成功读取文件。 The same process not working on server.相同的进程在服务器上不起作用。

Spring Boot version: 2.0.5.RELEASE Azure Web App as server. Spring 引导版本:2.0.5.RELEASE Azure Web 应用程序作为服务器。

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";
    }

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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