Skip to main content

Character string extraction from a text file

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
mond14
Offline
Joined: 2013-05-28

Hi,

Please I am a beginner in java, I want to extract information from a text file. is the following:
============================================
From: 55555565
From_SMSC: 78987555
Sent: 00-02-21 22:26:23
Received: 00-02-21 22:26:29
Subject: modem1
Alphabet: ISO
UDH: false

pr2
============================================

I want to extract the first line which is exactly the number " 55555565 " and the last line is "pr2".

for now I just use the code that allows me to extract only the last line"pr2".

++++++++++++++++++++++++++++++++++++++++++++
Scanner scanner = new Scanner(new File(/home/Desktop/ivw.txt));
String line = null;
while (scanner.hasNextLine()) {
line = scanner.nextLine();
}

readSMS=line;
scanner.close();
++++++++++++++++++++++++++++++++++++++++++++

I want to retrieve information both the first line and the last line so that I can put each in a variable.

thank you for your help

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Syed_Naved_Aali
Offline
Joined: 2013-11-28

scanner.nextLine(); reads one line every time your while loop executes so you have to write your logic in while loop.

try to print line object in while loop you will get idea of next step.