Spring中读取txt文件的方法 [英] method in spring to read txt file

查看:194
本文介绍了Spring中读取txt文件的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,我需要通过spring框架读取文本文件的内容.为此,我在服务实现类中创建了如下方法

I am having a requirement wherein I need to read the contents of a text file through spring framework. For this purpose I made a method in my service implementation class as below-

public String readFile(File file)

此方法将以文件名作为输入并读取文件.

This method will take the file name as input and read the file.

我正在用XML编写Spring的代码,如下所示-

I was writing the code in XML for spring as below-

<bean id="fstream" class="java.io.FileInputStream">
    <constructor-arg value="C:/text.txt" />
</bean>
<bean id="in" class="java.io.DataInputStream">
    <constructor-arg ref="fstream"/>
</bean>
<bean id="isr" class="java.io.InputStreamReader">
    <constructor-arg ref="in"/>
</bean>
<bean id="br" class="java.io.BufferedReader">
    <constructor-arg ref="isr"/>
</bean>

以下代码进入了我的方法-

Following code goes in my method-

public String readFile(File file)
{
    String line = null;
    String content = "";

    try
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("FileDBJob.xml");

        BufferedReader br = (BufferedReader) context.getBean("br");

        while((line = br.readLine())!=null)
            content = content.concat(line);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return content;
}

但是这里的问题是我需要用XML硬编码文件名,所以没有使用file参数.

But here the issue is that i need to hardcode the file name in XML, so there is no use of file parameter.

请协助寻找解决方案.由于我刚接触弹簧并尝试使用弹簧,因此我可能会丢失一些东西.任何帮助都会有很大帮助.

Kindly help in finding the solution. As I am new to spring and trying my hands with it so it may be possible that I am missing something. Any help would be of great help.

推荐答案

不要注入流和读取器,这实际上并不是打算使用Spring的方式.我会自己注入文件:

Don't inject the streams and readers, that's not really how Spring is intended to be used. I'd inject the file itself:

public class MyFileReader {

    private File file;

    public String readFile() {
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(getFile()));
            String line = null;
            while ((line = reader.readLine()) != null)
                builder.append(line);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeQuietly(reader);
        }
        return builder.toString();
    }

    private void closeQuietly(Closeable c) {
        if (c != null) {
            try {
                c.close();
            } catch (IOException ignored) {}
        }
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

}

然后,您的bean def如下所示:

Then your bean def looks like this:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:app.properties"/>
</bean>

<bean class="com.myapp.MyFileReader">
    <property name="file" value="${filePath}" />
</bean>

剩下的就是用正确的信息创建您的app.properties文件.您还可以通过使用-DfilePath=/foo/bar/whatever.txt

All that is left is to create your app.properties file with the correct info. You can also set the value by invoking the app with a -DfilePath=/foo/bar/whatever.txt

这篇关于Spring中读取txt文件的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆