如何从用户输入文件中输入文本进行解析 - Java

发布于 2024-11-13 23:42:22 字数 1529 浏览 3 评论 0原文

我已经学习Java几个月了,我面临着一个需要帮助的问题。我有一个用户输入文件,其中包含以下形式的许多条目:

>Item1 Description
DILDEQCLKGACQGTSVVIHTA
>Item2 Description
DILDEQCLKGACQGTSVVIHTASVIDVRNAV
>Item3 Description
AEKAGTS
>Item4 Description
RNAVPRHESAW

此文件中最多可能有 100 个项目。这是一个基本的文本文件,大写序列会根据项目的不同而有所不同。我认为其他读者/解析器依赖于“>”开始下一个条目。我想我也应该这样。

我的目标是从用户输入文件生成一个新文件,其中包含相同的 >ItemX Description 行,但字母顺序需要颠倒。我尝试过 Scanner、FileInputStream、DataInputStream 和 BufferedReader。不确定执行此操作的最佳方法是什么并且最有效。

我应该如何加载这些数据以便我可以最轻松地反转字符串?似乎输入数据的方法有很多,我似乎越来越迷失了...我有一个似乎可以工作的字符串反转器,所以我现在的目标是加载这些数据,然后转换为字符串为了逆转。

预先感谢您的任何帮助。

注意 - 这些条目之一可能如下所示:

>41_BOVIN (Q9N179) Protein 4.1 (Band 4.1) (P4.1) (4.1R) MHCKVSLLDDTVYECVVEKHAKGQDLLKRVCEHLNLLEEDYFGLAIWDNATSKTWLDSAK EIKKQVRGVPWNFTFNVKFYPPDPAQLTEDITRYYLCLQLRQDIVSGRLPCSFATLALLG SYTIQSELGDYDPELHGADYVSDFKLAPNQTKELEEKVMELHKSYRSMTPAQADLEFLEN AKKLSMYGVDLHKAKDLEGVDIILGVCSSGLLVYKEKLRINRFPWPKVLKISYKRSSFFI KIRPGEQEQYESTIGFKLPSYRAAKKLWKVCVEHHTFFRLTSTDTIPKSKFLALGSKFRY SGRTQAQTRQASALIDRPAPHFERTASKRASRSLDGAAAVEPADRTPRPTSAPAIAPSPA AEGGVPGAPVKKAQKETVQVEVKQEEAPPEDAEPEPSEAWKKKRERLDGENIYIRHSNLM LEDLDKSQEEIKKHHASISELKKNFMESVPEPRPSEWDKRLSTHSPFRTLNINGQIPTGE GPPLVKTQTVTISDTANAVKSEIPTKDVPIVHTETKTITYEAAQTDDSNGDLDPGVLLTA QTITSETTSSTTTTTQITKTVKGGISETRIEKRIVITGDADIDHDQVLVQAIKEAKEQHPD

所以我需要将第 2 行到第 12 行变成一个大字符串,然后反转它。

I have been learning Java for a few months now, and I am faced with a problem that I need some help on. I have a user input file that has many entries in the form:

>Item1 Description
DILDEQCLKGACQGTSVVIHTA
>Item2 Description
DILDEQCLKGACQGTSVVIHTASVIDVRNAV
>Item3 Description
AEKAGTS
>Item4 Description
RNAVPRHESAW

There might be up to 100 items in this file. It's a basic text file and the capitalized sequences will vary depending on the item. I think other readers/parsers rely on the '>' to start the next entry. I suppose I should too.

My goal is to produce a new file from the user input file that contains the same >ItemX Description line but the sequence of letters needs to be reversed. I have experimenting with Scanner, FileInputStream, DataInputStream, and BufferedReader. Not sure what the best way to do this would be and the most efficient.

How should I load these data so that I can reverse the string most easily? It seems there are so many ways to input data, I just seem to be getting more and more lost... I have a string reverser that seems to work, so my goal now is to get these data loaded and then convert to a string for reversal.

Thanks in advance for any assistance.

NOTE - here is what one of these entries might look like:

>41_BOVIN (Q9N179) Protein 4.1 (Band 4.1) (P4.1) (4.1R)
MHCKVSLLDDTVYECVVEKHAKGQDLLKRVCEHLNLLEEDYFGLAIWDNATSKTWLDSAK
EIKKQVRGVPWNFTFNVKFYPPDPAQLTEDITRYYLCLQLRQDIVSGRLPCSFATLALLG
SYTIQSELGDYDPELHGADYVSDFKLAPNQTKELEEKVMELHKSYRSMTPAQADLEFLEN
AKKLSMYGVDLHKAKDLEGVDIILGVCSSGLLVYKEKLRINRFPWPKVLKISYKRSSFFI
KIRPGEQEQYESTIGFKLPSYRAAKKLWKVCVEHHTFFRLTSTDTIPKSKFLALGSKFRY
SGRTQAQTRQASALIDRPAPHFERTASKRASRSLDGAAAVEPADRTPRPTSAPAIAPSPA
AEGGVPGAPVKKAQKETVQVEVKQEEAPPEDAEPEPSEAWKKKRERLDGENIYIRHSNLM
LEDLDKSQEEIKKHHASISELKKNFMESVPEPRPSEWDKRLSTHSPFRTLNINGQIPTGE
GPPLVKTQTVTISDTANAVKSEIPTKDVPIVHTETKTITYEAAQTDDSNGDLDPGVLLTA
QTITSETTSSTTTTQITKTVKGGISETRIEKRIVITGDADIDHDQVLVQAIKEAKEQHPD

So I would need to make lines 2 through 12 into one big string and then reverse that.

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

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

发布评论

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

评论(1

人生百味 2024-11-20 23:42:22

不要重新发明轮子! Apache commons 库为您解决了许多常见的编程问题。您可以使用以下两个类来解决您的问题:

List<String> lines = org.apache.commons.FileUtils.readLines(file);

它们为您完成所有 I/O 工作并以列表形式读取文件的行。
然后,您可以使用另一个 apache commons 类来进行逆转:

org.apache.commons.lang.StringUtils.reverse(str);

如果您使用 maven,则以下是依赖项:

对于 FileUtils 类(以及许多其他类):

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.0.1</version>
</dependency>

对于 StringUtils 类(以及许多其他类):

<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>

Don't reinvent the wheel! Apache commons libraries have many common programming problems solved for you. Here's two classes that you can use for your problem:

List<String> lines = org.apache.commons.FileUtils.readLines(file);

which does all the I/O work for you and reads the lines of the file in as a List.
Then you can use another apache commons class for the reversal:

org.apache.commons.lang.StringUtils.reverse(str);

If you use maven, here are the dependencies:

For the FileUtils class (and many others):

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.0.1</version>
</dependency>

For the StringUtils class (and many others):

<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文