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

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source