How to get characters from a line in a file ?
Hello Everybody ,
I am able to read the lines from the file using readLine( ) method.
But i do not want to read anything that comes after the character '#'
Any thing that follows a '#' character is considered as a commet in my case.
The contents of my input file are,
#Character mappings for Japanese Shift-JIS character set
#ASCII character Mapped Shift-JIS character
m 227,128,133 #Half width katakana letter small m
n 227,128,134 #Half width katakana letter small n
o 227,129,129
p 227,129,130
q 227,129,131
r 227,129,132
s 227,129,133
t 227,129,134
u 227,129,135
v 227,129,136
w 227,129,137
x 227,129,138
y 227,129,139
z 227,129,142
My code to read the contents of the file is,
import java.io.*;
class Read
public static void main(String s[])
try{
int i=0,j,l;
BufferedReader in=new BufferedReader(
new InputStreamReader(
new FileInputStream("Character.txt")));
String []str=new String[100];
while(true)
str=in.readLine();
if(str[i]==null) break;
System.out.println(str[i]);
i++;
catch(Exception e)
e.printStackTrace();
System.out.println(e);
How do i store the values within a line in the file
to two different variables
Like say,
I read the line
i get the character 'm' store it in a variable say, char ch='m' ;
and the corresponding value 227,128,133 in a String variable.
String str="227,128,133";
Please offer some suggesstions and help me out .
Any suggesstions would be welcome and very useful.
Thanks and regards
khurram
It depends on whether the format of the file will ever change. If the "rules" regarding the structure of the file are predictable, then it's fairly simple.
In the sample file, if we assume that anything that starts with # should be ignored, and all relevant lines begin with single char, followed by three comma separated integers; then you can just do the following (haven't tested this)
import java.io.*;
class Read
public static void main(String s[])
try{
// Create a Map to store the results..
Map data = new HashMap();
// We will store the actual values in an array
int[] values = null;
// Each valid line has a character
char theChar;
// Temp storage for the integer values
String[] strValues = null;
BufferedReader in=new BufferedReader(
new InputStreamReader(
new FileInputStream("Character.txt")));
String str=null;
while(true)
str=in.readLine();
if(str==null) break;
// trim to remove any spaces
str = str.trim();
theChar = str.charAt(0);
if(theChar != '#') {
// not a comment
// Split to get the int
strValues = str.substring(1, str.length()).split(",");
// We only want the first 3 tokens
values = new int[3];
for(int i = 0; i < values.length; i++) {
values[i] = Integer.parseInt(strValues);
// Now, add the results to the map
data.put(String.valueOf(theChar, values);
System.out.println(str);
catch(Exception e)
e.printStackTrace();
System.out.println(e);
This will leave you with a Map of the character mappings, keyed on the String value of the character (just because you can't store a char in a Map)
Similar Messages
-
How to get the number of lines of a file?
Folks:
Is there a way to get the number of lines of a text file? I don't know if there is an existing method to do that.
Thanks a lot.HI
I found some solution
other than increment loop and all
here is the code it might helpful to u
//returns the number of lines in a file
//author : Ravindra S
//Symphony software Hyderabad
try
RandomAccessFile randFile = new RandomAccessFile(csvFile,"r");
long lastRec=randFile.length();
randFile.close();
FileReader fileRead = new FileReader(csvFile);
LineNumberReader lineRead = new LineNumberReader(fileRead);
lineRead.skip(lastRec);
countRec=lineRead.getLineNumber()-1;
fileRead.close();
lineRead.close();
catch(IOException e) -
How to get rid of empty lines in a file?
How can read a file "text.txt", delete all the empty lines and restore it to same filename "text.txt" without using a temporary file?
Ages ago I ruined an 8" floppy disk drive by doing something similar to
this: let b1 b2 ... bn nl be a series of blanks followed by a newline
character (nl) and let c1 ... cm be a sequence of other characters. Let
the total relevant sequence be:
b1 b2 ... bn nl c1 ... cm
Reverse the entire sequence: cm ... c1 nl bn ... b2 b1 and then
reverse the first m bytes: c1 cm nl bn ... b2 b1. Now repeat the entire
thing for the sequence c1 ... cm by checking for more blank lines in it.
After this has all finished, simply truncate the file.
The advantage of this method was that I only needed to be able to swap
two bytes in a file; the disadvantage was that the floppy disk drive
couldn't handle it ;-)
kind regards,
Jos -
How to get data from BW Cube to Another File system?
Hi,
I have a requirement to get the sales data from BW system(infoCube) to another file system as XML file using XI as a middleware. Can anybody give the suggestions on this please?
Thanks & Regards,
SatishTake a look at the following document. You will get a fair idea how to integrate BW and XI.
BW > XI
https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/biw/g-i/integrate bw via xi.pdf
XI> BW
https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/biw/g-i/how to push data into bw from xi.pdf
regards
SKM -
Hey guys,
I need to read in a file but strip the first 7 characters from each line in the file???
How do i do it...
Any help will be appreciated...Dear Friend,
Thankyou very much for your interest.
I am trying to strip the 1st 7 characters from the following file while reading....
1. <?xml version="1.0" encoding="UTF-8"?>
2.
3. <DOCUMENT>
4. <GREETING>
5. THE LIVE EXAMPLE...
6. <MESSAGE>
7. </GREETING
8. If this works then your project is over...;;;
9. </MESSAGE>
10. </DOCUMENT>
I meant, if my file contains line break (space of a line between two lines just like in Double Line spacing. Eg. between 1 and 3).... the program doesn't read after reading the first line. Your method does stip first seven characters of first line...
Your help is appreciated.... -
How to get string (specified by line and column) from txt file with labview
Hi everyone
How to get string (specified by line and column) from txt file with labview
thx
Solved!
Go to Solution.As far as I know, a text file has no columns. Please be more specific. Do you mean something like the 5th word on line 4, where words are separated by a space, and lines are separated by a newline character? You could the Read from Spreadsheet String function and set the delimiter to a space. This will produce a 2D array of strings. Then use index array and give the line number and column number.
- tbob
Inventor of the WORM Global -
HT1338 How to get updgrade from OS X 10.5.8 on line?
How to get updgrade from OS X 10.5.8 on line?
Phone the online Apple Store and order Mac OS X 10.6 on DVD. No downloads for it are available to the general public.
(70458) -
How to get BOOLEAN from STORED FUNCTION
We are calling legacy PLSQL stored procedures and functions via named queries. This has worked fine so far, but there are some functions which return the type 'BOOLEAN'. e.g.
FUNCTION some_function( some_argument IN NUMBER) RETURN BOOLEAN;
Where the return type is BOOLEAN calling the named query fails with
Local Exception Stack:
Exception [TOPLINK-4002] (Oracle TopLink - 10g Release 3 (10.1.3.0.0) (Build 060118)): oracle.toplink.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: ORA-06550: line 1, column 13:
PLS-00382: expression is of wrong type
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
Error Code: 6550
A couple of threads have hinted that what we are trying to do is not possible:
How to get BOOLEAN from STORED PROCEDURES
Re: Creating Named Query: from OracleCallableStatement
This would possibly be due to 'restriction in the OCI layer'. Can anyone help? Is there really now way to call a valid PLSQL stored function via a named query when the return type is BOOLEAN?
thanksI can't comment on possible issues you might have with the driver, but if it can be done in JDBC, it should be possible in TopLink.
TopLink has the StoredFunctionCall which extends the StoredProcedureCall but adds an unnamed ouput parameter in the first spot of its parameter list. You will need to get the databasefield and set its type to BOOLEAN ie:
DatabaseField returnField = (DatabaseField)yourStoredFunctionCall.getParameters().firstElement();
returnField.setName(name);
returnField.setSqlType(Type.BOOLEAN);Be sure not to use the setType() method, as I believe TopLink will try to use the Type.BIT when a boolean class is used as the classtype.
Best Regards,
Chris -
How to clean project from comman line ?
How to clean project from comman line ?
I would like to invoke the same functionality as in Flex
Builder Project -> clean... -> clean all projects...
but from command line is it possible ?You can:
1) Perform a project clean up (p.153 of the user manual) - it removes any unused recordings, samples and other files
2) Consolidate project (p. 154 of manual) - this copies all the used audio material into the project folder and also deletes any empty folders within project folder, making it easier to navigate
3) Save project as a copy - this creates another project folder, but without all the backups and other additional files -
how to get photos from mac to iPhone 6? when i try it just comes up iCloud instead of my photos
Make sure Settings > iCloud > Photos > iCloud Photo Library (Beta) = "Off"
Then sync your iPhone using iTunes and make sure your sync settings for photos are set correctly in iTunes. -
I got a new computer and don't know how to get music from ipod touch to itunes library
I recently got a new computer and don't know how to get music from my ipod touch to show on my itunes library.
You need to transfer the iTunes folder from the old computer to the new one.
iTunes: How to move your music to a new computer (Mac or Windows):
http://support.apple.com/kb/HT4527 -
I have a new MacBook Air and don't know how to get info from my USB stick and get info from my SD card. Can anyone help, please?
Plug the stick and/or card into the appropriate slots on the side of your Air. Do you see icons for the devices appear on the desktop? Click into them to see what files are there.
Matt -
How to get data from a USB-UIRT device using Labview?
How to get data from a USB-UIRT device using Labview?
I'm trying to get data from a USB-UIRT device, is it posible with Labview?
I really appreciate your help,
thanksYou may want to contact the developer of the device for the API and DLL.
http://65.36.202.170/phpBB2/viewforum.php?f=3 -
How to get value from list item
Hi all,
How to get value from list item?
I have a problem with the List Item object
in the Oracle forms.
How can I retrieve the selected item from
a list ?
I didn't find any function like 'list.GET_
SELECTED_ITEM()'...
thanks
BalaHello,
You get the value as for any other Forms item:
:value := :block.list_tem ;Francois -
How to get FILENAME from FILE PATH
does anyone know how to get filename from a file path for example
FILE PATH: C:\Project\uploadbean\web\uploads\Button.txt
returns
FILENAME: Button.txt@BalusC
ust for a reference cause i'm new in JSP This has nothing to do with JSP, but with basic knowledge of an essential API. I have given you the link to the File API. Are you saying that you refused to read the API documentation, which clearly explains you how to use the File and shows which methods are all available to you undereach the straightforward getName() method, and expecting that the others may chew the answers for you? Loser.
Maybe you are looking for
-
How to preset the filename for an exported file?
Post Author: yekken83 CA Forum: General Hi all, Generally, the exported file name will defaulted to report name. However, anyone know that can i change the exported file name to another specific file name which different as my report name? The export
-
I need some assistance with my Adobe. It is asking me to upgrade and purchase, when this has never happened to me before. I am taking classes through the art institute and the only way to submit my papers are in a pdf. I had paid with my school money
-
every time I go to a new site a bar is lowered and ask to update my plugin. I am using mac 10.4.11 G4 The new plugin will not work. How do I shut off the plugin update so I don't get this anoying bar ever time I go to a new web page. If there is no w
-
Freight Charges after Final Invoice(MIRO)
Hi SAP Gurus, We have received one freight invoice after accounting of final invoice. How can we pay this freight amount to the vendor via same PO which has been fully invoiced. Thanks & Regards. Anil Thakur
-
Windows 2008 R2 DC VM's hang at start-up
I have 2 Virtual Domain Controllers, they login fine for about 4 weeks after a reboot. After about 4 weeks when logging in via Remote Desktop then they stay at the welcome screen, or they hang at the Group Policy phase of the log in. Now I have one