Answers for "Read File from Sprinboot"

0

Read File from Sprinboot

Using ResourceUtil

@Override
	public void run(String... args) throws Exception {
		File file = ResourceUtils.getFile("classpath:data.txt");
		if(file.exists()) {
			byte[] fileData = Files.readAllBytes(file.toPath());
			String fileContent = new String(fileData);
			
			logger.info("data.txt file content:");
			logger.info(fileContent);
		}
	}
///////////////////////////////////////////////////////////
import org.springframework.util.FileCopyUtils;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

// ...

InputStream is = new ClassPathResource("/instructions.txt").getInputStream();
try {
    String contents = new String(FileCopyUtils.copyToByteArray(is), StandardCharsets.UTF_8);
    System.out.println(contents);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (is != null) {
        is.close();
    }
}


//////////////////////////////////////////////////////////

Using ClassPathResource

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.FileCopyUtils; 
 
@SpringBootApplication
public class Application implements CommandLineRunner
{
    final Logger LOGGER = LoggerFactory.getLogger(getClass());
             
    public static void main(String[] args) 
    {
        SpringApplication app = new SpringApplication(Application.class);
        app.run(args);
    }
 
    @Override
    public void run(String... args) throws Exception 
    {
        Resource resource = new ClassPathResource("classpath:data.txt");
        InputStream inputStream = resource.getInputStream();
        try {
            byte[] bdata = FileCopyUtils.copyToByteArray(inputStream);
            String data = new String(bdata, StandardCharsets.UTF_8);
            LOGGER.info(data);
        } catch (IOException e) {
            LOGGER.error("IOException", e);
        }
    }
}



//////////////////////////////////////////////////////////////////////


Using Resource Loader------>
  
  
  final Logger LOGGER = LoggerFactory.getLogger(getClass());
     
@Autowired
ResourceLoader resourceLoader;
 
@Override
public void run(String... args) throws Exception 
{
    Resource resource = resourceLoader.getResource("classpath:data.txt");
    InputStream inputStream = resource.getInputStream();
 
    try
    {
        byte[] bdata = FileCopyUtils.copyToByteArray(inputStream);
        String data = new String(bdata, StandardCharsets.UTF_8);
        LOGGER.info(data);
    } 
    catch (IOException e) 
    {
        LOGGER.error("IOException", e);
    }
}
Posted by: Guest on July-24-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language