Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 679 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
read file in classpath

#1
Here is what I want to do and I am wondering if there is any Spring classes that will help with implementing. I don't have to use spring for this particular problem, I'm just implementing it with everything else.

In my DAO layer I want to externalize my sql files aka 1 sql per file. I want to read and cache the sql statement even maybe as a spring bean singleton. But in my initial struggles, I am having a problem just loading a sql file in the classpath...

Is there anything in spring to help with that? I've been through the documentation but nothing is jumping out at me.

Here is kind of what I'm after.. but I can't get it to recognize the file or maybe the classpath... not real sure does something need to be defined in applicationContext?

Here are a couple of attempts that do not seem to work... both spring'ish and just java'ish.

reader = new BufferedReader(new InputStreamReader(new ClassPathResource("com.company.app.dao.sql.SqlQueryFile.sql").getInputStream())

reader = new BufferedReader(new InputStreamReader(ClassLoader.getSystemResourceAsStream("com.company.app.dao.sql.SqlQueryFile.sql")));

Any thoughts?
Reply

#2
Try getting Spring to inject it, assuming you're using Spring as a dependency-injection framework.

In your class, do something like this:

public void setSqlResource(Resource sqlResource) {
this.sqlResource = sqlResource;
}

And then in your application context file, in the bean definition, just set a property:

<bean id="someBean" class="...">
<property name="sqlResource" value="classpath:com/somecompany/sql/sql.txt" />
</bean>

And Spring should be clever enough to load up the file from the classpath and give it to your bean as a resource.

You could also look into [PropertyPlaceholderConfigurer][1], and store all your SQL in property files and just inject each one separately where needed. There are lots of options.


[1]:

[To see links please register here]

Reply

#3
Change . to / as the path separator and use `getResourceAsStream`:


reader = new BufferedReader(new InputStreamReader(
getClass().getClassLoader().getResourceAsStream(
"com/company/app/dao/sql/SqlQueryFile.sql")));

or

reader = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream(
"/com/company/app/dao/sql/SqlQueryFile.sql")));

Note the leading slash when using `Class.getResourceAsStream()` vs `ClassLoader.getResourceAsStream`.
`getSystemResourceAsStream` uses the *system* classloader which isn't what you want.

I suspect that using slashes instead of dots would work for `ClassPathResource` too.
Reply

#4

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class readFile {
/**
* feel free to make any modification I have have been here so I feel you
*
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
File dir = new File(".");// read file from same directory as source //
if (dir.isDirectory()) {
File[] files = dir.listFiles();
for (File file : files) {
// if you wanna read file name with txt files
if (file.getName().contains("txt")) {
System.out.println(file.getName());
}

// if you want to open text file and read each line then
if (file.getName().contains("txt")) {
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(
file.getAbsolutePath());
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(
fileReader);
String line;
// get file details and get info you need.
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
// here you can say...
// System.out.println(line.substring(0, 10)); this
// prints from 0 to 10 indext
}
} catch (FileNotFoundException ex) {
System.out.println("Unable to open file '"
+ file.getName() + "'");
} catch (IOException ex) {
System.out.println("Error reading file '"
+ file.getName() + "'");
// Or we could just do this:
ex.printStackTrace();
}
}
}
}

}`enter code here`

}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through