How to Input Text From User Input File for Parsing - Java
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 (Q9N17开发者_如何学C9) 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.
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>
精彩评论