Needs to access csv files in src/test/resource folder

java get src/main/resources path
how to read a file from src folder in java
java read file from resources
src/main/resources file not found
get resource folder path in java

I have the requirement to read CSV file from src/test/resource folder.

I provided the file at the root level and it is working fine. But I want to put all these files in src/test/resource folder.

String fileName = "Phone_valid.csv";
File srcFile = new File(fileName);
String path = srcFile .getAbsolutePath();

// path = C:\D Drive\Orange Codebase\dabek-data-processing\Phone_valid.csv


String fileName = "\\src\\main\\resource\\Phone_invalid.csv";
File srcFile = new File(fileName);
String path = srcFile .getAbsolutePath();

// path= "C:\src\main\resource\Phone_invalid.csv"


I want that path should be 
path = C:\D Drive\Orange Codebase\dabekdataprocessing\\src\main\resource\Phone_invalid.csv

Remove the first backslash. Does this work?

String fileName = "src\\main\\resource\\Phone_invalid.csv";

Java - Read a file from resources folder, Read File and Resource in JUnit Test - Sample Project Directory test class in the test sources (src/test/java) will need to read a file For example, we will read the users.csv file from the src/test/resources/data01 directory:. Sometimes during unit testing, we might need to read some file from the classpath or pass a file to an object under test. Or, we may have a file in src/test/resources with data for stubs that could be used by libraries like WireMock. In this tutorial, we'll show how to read the path of the /src/test/resources directory.

This should work.

File file= new File("src/test/resources/filename.txt");

Note Avoid Space in directory name like Orange Codebase

Read File and Resource in JUnit Test, You must organize files of different types into the folders reserved for them by the Maven A LiveView fragment project adds src/main/liveview to this structure. Any CSV file used as input for a feed simulation, or used by any operator or adapter. This folder holds any test-specific resource files needed by the StreamBase  This quick tutorial is going to cover how to read file and resource in JUnit test where may need to access files or resources under the src/test/resources folder. 1. Sample Project Directory Layout. Let’s take a look at a sample project directory layout of a project called junit5-tutorial which contains some JUnit 5 examples.

Try this code

import java.io.File;

public class MyClass {

    public static void main(String[] args) {
        String fileName = "src\\main\\resource\\Phone_invalid.csv";
        File srcFile = new File(fileName);
        String path = srcFile .getAbsolutePath();
        System.out.println("Path: "+path);
    }
}

Output of above code will be..

Path: E:\Orange Codebase\dabekdataprocessing\src\main\resource\Phone_invalid.csv

What Goes Where in Project Folders, A resource is a file in the class path folder structure for your project. src\main\​resources for resources available to my main 'live' code. For example, I have a test called LinkCheckerTest and it is in a package called: I could access a resource in the folder structure from within the LinkCheckerTest using:. src\test\resources for resources available to my test code. The folder hierarchy below the resources is my package hierarchy. For example, I have a test called LinkCheckerTest and it is in a

I achieved the same by using below code

String phonePath= "src/test/resources/Phone_valid.csv";
File srcFile = new FileSystemResource(phonePath).getFile();

String path = srcFile.getAbsolutePath();

// path = C:\D Drive\Orange Codebase\dabekdataprocessing\src\test\resources\Phone_valid.csv.

Now, I am able to access the file from src/test/resources folder.

Organizing Resource Files for Tests Using Maven and Java, Sometimes during unit testing, we might need to read some file from the classpath or pass a file to an object under test. Or, we may have a file  In Windows Server 2012, you cannot enable data deduplication or compression on CSV. Resource type in the cluster. For a CSV volume, you must use the Physical Disk resource type. By default, a disk or storage space that is added to cluster storage is automatically configured in this way. Choice of CSV disks or other disks in cluster storage

Get the Path of the /src/test/resources Directory in JUnit, Quick code example of the various ways we can read a file with Java. Path path = Paths.get( "src/test/resources/fileTest.txt" );. String read Note that you can use the readAllBytes() method as well if you need binary data. In this tutorial, we will show you how to read a file from a resources folder or classpath, in both runtime and unit test environment. Try putting a file into the src/main/resources folder, and read the file with following code snippets : 1. getResource

How to Read a File in Java, To elaborate, suppose you have the following files in src/test/resources : 01.json containing JSON content: { "name": "John Doe", "age": 30 }. Hi @j_w ,. Based on my test, it might be caused that you didn't sign in the organizational account. Please try to follow the steps. 1. Click File->Options and Settings->Data source settings->clear your previous permission.

How to get Maven to read a file from my resources folder, The full folder structure for a resource file used by the LinkCheckerTest would be: - src - test - resources - com - javafortesters - course - exercises - casestudy - casestudy_002_buildAnHttpLinkChecker I could access a resource in the folder structure from within the LinkCheckerTest using

Comments
  • it is not working by removing the slash
  • You want to access file in test folder. It means you have to run it in test context. How about running your code under src/test/java and not src/main/java?
  • You say 'it is not working by removing the slash'. What output you expect? What output you receive?
  • Actually i am getting path= "C:\src\main\resource\Phone_invalid.csv" but i need C:\D Drive\Orange Codebase\dabekdataprocessing\\src\main\resource\Phone_invalid.csv.
  • What is it printing if you use the suggested fileName ("src\\main\\resource\\Phone_invalid.csv")? What is your working directory?