删除从输入文件到 Spring Batch 程序的所有行中的尾随空格

发布于 2024-12-18 18:08:42 字数 2677 浏览 3 评论 0原文

我有一个 Spring Batch 程序,它从文件读取并写入数据库。

数据由 || 分隔(双管道)并且行尾没有分隔符。

one||two||three||four
foo||bar||foo1||bar1

问题是每行的末尾在行尾字符之前有数百个尾随空格。

使用 ctrl + Q,我在文本板中显示:

one||two||three||four.......................................
foo||bar||foo1||bar1...............
car||bike||tango||charlie..........................

这是在 ItemReader (org.springframework.batch.item.file.FlatFileItemReader) 中的每一行抛出解析错误

我无法手动删除空格并需要一些自动化,以便在文件之前由 spring 批处理程序读取,空格消失了。

如何做到这一点?

注意:仅当我在 unix 服务器上运行程序时才会出现该错误。它不会发生在 Windows 上。

<beans:bean id="myBufferedReaderFactory" class="com.mypackage.MyBufferedReaderFactory"/>

<beans:bean id="FileToDBItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
                 <beans:property name="BufferedReaderFactory" ref="myBufferedReaderFactory"/>
        <beans:property name="resource" ref="MyInputFileResource" />
        <beans:property name="lineMapper">
            <beans:bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                <beans:property name="lineTokenizer">
                    <beans:bean class="com.mypackage.MyCustomLineTokenizer">
                        <beans:property name="delimiter" value="||"/>
                        <beans:property name="names" value="one,two,three,four" />
                    </beans:bean>
                </beans:property>
                <beans:property name="fieldSetMapper">
                    <beans:bean class="com.mypackage.MyFieldSetMapper" />
                </beans:property>
            </beans:bean>
        </beans:property>
    </beans:bean>

进步了一点。但仍然没有找到答案。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.springframework.batch.item.file.BufferedReaderFactory;
import org.springframework.core.io.Resource;


public class MyBufferedReaderFactory implements BufferedReaderFactory {


    public BufferedReader create(final Resource resource, final String encoding) throws UnsupportedEncodingException, IOException {

        InputStream sourceStream = null;
        sourceStream = resource.getInputStream();

        //code to remove trailing spaces end

        //code to remove trailing spaces start

        return new BufferedReader(new InputStreamReader(sourceStream, encoding));

    }

}

如何使用上面类中的 InputStream 对象删除尾随空格? 感谢您的阅读!

I have a spring batch program which reads from a file and writes to a database.

Data is delimited by || (double pipes) and there is no delimiter at end of line.

one||two||three||four
foo||bar||foo1||bar1

Issue is the end of each line is having hundreds of trailing spaces before end of line character.

Using ctrl + Q, I in text pad shows:

one||two||three||four.......................................
foo||bar||foo1||bar1...............
car||bike||tango||charlie..........................

This is throwing parsing error at each line in ItemReader (org.springframework.batch.item.file.FlatFileItemReader)

I can not remove the spaces manually and need some automation so that before the file is read by spring batch program, the spaces are gone.

How to do this ?

Note: The error occurs only when I run the program over unix server. It does not happen on windows.

<beans:bean id="myBufferedReaderFactory" class="com.mypackage.MyBufferedReaderFactory"/>

<beans:bean id="FileToDBItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
                 <beans:property name="BufferedReaderFactory" ref="myBufferedReaderFactory"/>
        <beans:property name="resource" ref="MyInputFileResource" />
        <beans:property name="lineMapper">
            <beans:bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                <beans:property name="lineTokenizer">
                    <beans:bean class="com.mypackage.MyCustomLineTokenizer">
                        <beans:property name="delimiter" value="||"/>
                        <beans:property name="names" value="one,two,three,four" />
                    </beans:bean>
                </beans:property>
                <beans:property name="fieldSetMapper">
                    <beans:bean class="com.mypackage.MyFieldSetMapper" />
                </beans:property>
            </beans:bean>
        </beans:property>
    </beans:bean>

Progressed a bit. But still not hitting the answer.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.springframework.batch.item.file.BufferedReaderFactory;
import org.springframework.core.io.Resource;


public class MyBufferedReaderFactory implements BufferedReaderFactory {


    public BufferedReader create(final Resource resource, final String encoding) throws UnsupportedEncodingException, IOException {

        InputStream sourceStream = null;
        sourceStream = resource.getInputStream();

        //code to remove trailing spaces end

        //code to remove trailing spaces start

        return new BufferedReader(new InputStreamReader(sourceStream, encoding));

    }

}

How can I remove the trailing spaces using InputStream object in above class ??
Thanks for reading!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

無處可尋 2024-12-25 18:08:42

通过实现 BufferedReaderFactory 来扩展 BufferedReader 并向读取器添加行修剪功能。请参阅FlatFileItemReader.setBufferedReaderFactory()

public class LineTrimmingBufferedReader extends BufferedReader
{
    public LineTrimmingBufferedReader(Reader in)
    {
        super(in);
    }

    @Override
    public String readLine() throws IOException
    {
        String result = super.readLine();
        return result != null ? result.trim() : null;
    }
}

如果 String.trim() 不可接受,请对行进行适当的尾部空格修剪。

Extend BufferedReader by implementing BufferedReaderFactory and add line-trimming functionality to the reader. See FlatFileItemReader.setBufferedReaderFactory().

public class LineTrimmingBufferedReader extends BufferedReader
{
    public LineTrimmingBufferedReader(Reader in)
    {
        super(in);
    }

    @Override
    public String readLine() throws IOException
    {
        String result = super.readLine();
        return result != null ? result.trim() : null;
    }
}

Implement proper trailing space trimming of the line if String.trim() isn't acceptable.

魂归处 2024-12-25 18:08:42

代码如下:

BufferedReader reader = ....
String line = null;

while((line = reader.readLine()) != null) {
    line = line.replaceAll("\\s*$");
    // do what you need with the line
}

如果您有一些自动过程读取文件并将所有内容插入数据库,并且您无法修改该过程,请创建读取文件并删除尾随空格(如上所示)并将修剪后的行写入其他文件的代码。这个其他文件可以作为数据库导入过程的输入。

顺便说一句,如果你使用的是 Linux,你可以直接说:

cat myfile.txt | sed 's/ *$//' > mytrimmedfile.txt

(可能您必须在此语句中转义 *

Here is the code:

BufferedReader reader = ....
String line = null;

while((line = reader.readLine()) != null) {
    line = line.replaceAll("\\s*$");
    // do what you need with the line
}

If you have some automatic procedure that reads files and inserts everything to DB and you cannot modify the procedure, create code that reads files and removes trailing spaces (as shown above) and writes the trimmed lines to other file. This other file can be an input for your DB import procedure.

BTW if you are using linux you can just say:

cat myfile.txt | sed 's/ *$//' > mytrimmedfile.txt

(probably you have to escape * in this statement)

绻影浮沉 2024-12-25 18:08:42

另一个解决方案。将配置中的 DefaultLineMapper 替换为以下自定义类 DefaultTrimmingLineMapper。

public class DefaultTrimmingLineMapper<T> extends DefaultLineMapper<T> {

    /** 
     * Trims the line before passing it to the {@link DefaultLineMapper}.
     * 
     * @see org.springframework.batch.item.file.mapping.DefaultLineMapper#mapLine(java.lang.String, int)
     */
    @Override
    public T mapLine(String line, int lineNumber) throws Exception {
        return super.mapLine(line!=null?line.trim():null, lineNumber);
    }
}

Another solution. Replace the DefaultLineMapper in configuration with following custom class DefaultTrimmingLineMapper.

public class DefaultTrimmingLineMapper<T> extends DefaultLineMapper<T> {

    /** 
     * Trims the line before passing it to the {@link DefaultLineMapper}.
     * 
     * @see org.springframework.batch.item.file.mapping.DefaultLineMapper#mapLine(java.lang.String, int)
     */
    @Override
    public T mapLine(String line, int lineNumber) throws Exception {
        return super.mapLine(line!=null?line.trim():null, lineNumber);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文