Read a file chracter by character
Right now I am using a String tokenizer to read the file, which is giving me code in tokens, while what I need is to get the file character by character. I'm not sure if there is a way to make the tokenizer read character by character or not, also I have looked into using into using the Scanner class, but sadly this is an assignment for school and the school has not updated their java platform in sometime so the scanner class is not available.
Here is my code thus far:
StringTokenizer entry;
System.out.println ("Input file name to be analyzed:");
BufferedReader keyboardentry = new BufferedReader
(new InputStreamReader (System.in));
entry = (new StringTokenizer(keyboardentry.readLine()));
filename = entry.nextToken();
File dataFile = new File(filename);
BufferedReader fileReader2 = new BufferedReader
(new InputStreamReader
(new FileInputStream (dataFile)));
while(fileReader2.ready())
fileLine = fileReader2.readLine();
if(fileLine != null)
dataLine += (fileLine + " ");
// Output input file
//System.out.println ("Input file: ");
//system.out.println (dataLine);
// Load symbols to linked list
StringTokenizer tokenizer3 = new StringTokenizer (dataLine);
try {
while (tokenizer3.hasMoreTokens())
temp = tokenizer3.nextToken();
catch (NoSuchElementException e)
System.out.println(e);
}
Why are you building a String which you then disect into a list.
Why not just put the lines into a list.
For you interest...
File file = new File(pathname);
byte[] fileData = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(fileData);
fis.close();
List lines = Arrays.asList(new String(fileData).split("\\r?\\n"));
for(int j=0;j<lines.size();j++)
System.out.println(j+": "+lines.get(j));
Similar Messages
-
How to read text file contain chinese character ?
Hi XI Expert,
I have scenario to read text file contain Chinese Character using sender file adapter. but every i was check from sxmb_moni all the chinese character looks different. and also the target file also i has changes.
Please advise me how to maintaine the chinese character in PI 7.0 SP17.
Thank You and Best Regards
FernandHi,
Refer these threads..
Receiver file adapter corrupting characters
Problem in converting special characters in input text file
Here is one more useful guide..
https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/502991a2-45d9-2910-d99f-8aba5d79fb42
Regards,
Sarvesh
Edited by: Sarvesh Singh on Nov 17, 2008 3:47 PM -
Problem reading csv file with special character
Hai all,
i have the following problem reading a csv file.
442050-Operations Tilburg algemeen Huis in t Veld, EAM (Lisette) Gebruikersaccount 461041 Peildatum: 4-5-2010 AA461041 1 85,92
when reading this line with FM GUI_UPLOAD this line is split up in two lines in data_tab of the FM,
it is split up at this character
Line 1
442050-Operations Tilburg algemeen Huis in
Line 2
t Veld, EAM (Lisette) Gebruikersaccount 461041 Peildatum: 4-5-2010 AA461041 1 85,92
Anyone have a idea how to get this in one line in my interbal table??
Greetz RichardHi Greetz Richard
Problably character contains same binary code as line feed + carriage return.
You can use statement below as workaround.
OPEN DATASET file FOR INPUT IN TEXT MODE ENCODING UNICODE
In this case your system must support Unicode encoding
Kind regards -
Read Document in FileUtilities cannot search a file with french character in the file name
Hi,
I am trying to search a file with french character in the file name like 'captures écran.doc' in my unix server directory, and Write the file somewhere, say in a list of documents or, write on file system to another directory.
I am using to Read Document in FileUtilities of Foundation, but it cannot read the file due to french character in its name. Although it can find any other file name without these french characters.
Tried renaming but that also cannot find the file with french character name.
Please provide any idea to solve it using LC operation.
Regards,
Rohan Raj.Hi Thomas,
Thanks for the post, but I have already found a solution to it a month ago. Sorry for not posting the solution.
You just have to set the '-Djava.property.file.encoding=ISO8859-1' into JVM argument of your server startup, and bounce the server back to pick the new JVM arg set. And now the service Read Document in FileUtilities of Foundation will read all french characters.
ISO8859-1refers to "Latin alphabet no. 1," consisting of 191 characters from the Latin script. This character-encoding scheme is used throughout The Americas,Western Europe, Oceania, and much of Africa. It is also commonly used in most standard romanizations of East-Asian languages.
regards,
Rohan Raj. -
How can I read a file with ASCII Special Character into a SQL table using SSIS 2008?
I've tried everything to read this file and am getting no where. Help how can I read this file and load a SQL table?
RS - AscII - 30 (Record Separator)
GS - AscII - 29 (Group Separator)
Thank you for your assistance - Covi
Mark CovianWe can use script component as source/transformation to read the text file and assign the contains to a string. Split the string by chr(30) i.e RS and finally stored into an array or write to the output buffer of the script component.
For example on how to use script component refer this link
http://social.technet.microsoft.com/Forums/en-US/6ff2007d-d246-4107-b77c-624781baab38/how-to-use-substring-in-derived-column-in-ssis?forum=sqlintegrationservices
Regards, RSingh -
How to read the file from a folder.
Hi All,
How to read the file from a folder or directory from the non sap server / remote server.
Regards
Sathisopen dataset filename for input in text mode
encoding default.
filename is character type variable with the destination filename.
Edited by: Jino Augustine on Apr 19, 2010 1:31 PM -
Regarding Reading the file from Application Server
Hi,
I am trying to read data from Application Server but due to special characters it is getting dumped out.
US24,Q,Acero (Carbon),AA,0010,0001,01,Ver Mir para dimension#
US24,Q,Acero (Carbon),AA,0010,0002,01,Area rectificada sin da#os ra
US24,Q,Acero (Carbon),AA,0010,0003,01,Ver Mir para dimension#
US24,Q,Acero (Carbon),CD,0010,0001,01,ITPE Soken para verificacion
US24,Q,Acero (Carbon),CD,0010,0010,01,No se permite desprendimiento
US24,Q,Acero (Carbon),CD,0010,0002,01,"Vernier, cinta metrica"#
In the last line it is going into dump due to special character ".
Please suggest.
Thanks
Priyanka.Hi,
You can check the mode in which you are reading the file
from application server that is
Open Dataset in Binary or Text Mode.
Hope it helps
Regards
Mansi -
Reading one file at a time using File Adapter
I am using a File Adapter which polls every minute to read files from a Folder. This process picks up all the files in that folder. What I want to achieve is that the File Adapter should read one file at a time. Is it possible to force it to wait for a few seconds before reading the Next file in the Folder. Or is there a solution to what I am trying to achieve? Any help would be appreciated.
public static char readChar() {
int ch;
char r = '\0';
boolean done = false;
while (!done) {
try {
ch = System.in.read();
r = (char) ch;
if (r == '\r') {
System.out.println("Not a character. Please try again!");
System.in.skip(1);
done = false;
} else {
System.in.skip(2);
done = true;
} catch (java.io.IOException e) {
done = true;
return r;
Try this as an method and then use it..
This code will read will be in response untill you hit the return key but will take only the first character.... -
Error while reading excel file from application server into internal table.
Hi experts,
My requirement is to read an excel file from application server into internal table.
Hence I have created an excel file fm_test_excel.xls in desktop and uploaded to app server using CG3Z tcode (as BIN file type).
Now in my program I have used :
OPEN DATASET v_filename FOR INPUT IN text mode encoding default.
DO.
READ DATASET v_filename INTO wa_tab.
The statement OPEN DATASET works fine but I get a dump (conversion code page error) at READ DATASET statement.
Error details:
A character set conversion is not possible.
At the conversion of a text from codepage '4110' to codepage '4103':
- a character was found that cannot be displayed in one of the two
codepages;
- or it was detected that this conversion is not supported
The running ABAP program 'Y_READ_FILE' had to be terminated as the conversion
would have produced incorrect data.
The number of characters that could not be displayed (and therefore not
be converted), is 445. If this number is 0, the second error case, as
mentioned above, has occurred.
An exception occurred that is explained in detail below.
The exception, which is assigned to class 'CX_SY_CONVERSION_CODEPAGE', was not
caught and
therefore caused a runtime error.
The reason for the exception is:
Characters are always displayed in only a certain codepage. Many
codepages only define a limited set of characters. If a text from a
codepage should be converted into another codepage, and if this text
contains characters that are not defined in one of the two codepages, a
conversion error occurs.
Moreover, a conversion error can occur if one of the needed codepages
'4110' or '4103' is not known to the system.
If the conversion error occurred at read or write of screen, the file
name was '/usr/sap/read_files/fm_test_excel.xls'. (further information about
the file: "X 549 16896rw-rw----201105170908082011051707480320110517074803")
Also let me know whether this is the proper way of reading excel file from app server, if not please suggest an alternative .
Regards,
KarthikHi,
Try to use OPEN DATASET v_filename FOR INPUT IN BINARY mode encoding default. instead of OPEN DATASET v_filename FOR INPUT IN text mode encoding default.
As I think you are uploading the file in BIN format to Application server and trying to open text file.
Regards,
Umang Mehta -
What are the commands available to read a file from application server and
What are the commands available to read a file from application server and store the file into an internal table?
Hi,
To read a file from an Application Server to an Object there is a command in ABAP called <b>READ DATASET</b>. After that file is transported to that object you have to do a loop and put that data in an Internal Table.
This statement exports data from the file specified in dset into the data object dobj. For dobj, variables with elementary data types and flat structures can be specified. In Unicode programs, dobj must be character-type if the file was opened as a text file.
For dset, a character-type data object is expected - that is, an object that contains the platform-specific name of the file. The content is read from the file starting from the current file pointer. After the data transfer, the file pointer is positioned after the section that was read. Using the MAXIMUM LENGTH addition, the number of characters or bytes to be read from the file can be limited. Using ACTUAL LENGTH, the number of characters or bytes actually used can be determined.
In a Unicode program, the file must be opened with an arbitrary access type; otherwise, an exception that cannot be handled will be triggered.
If the file has not yet been opened in anon-Unicode program, it will be implicitly opened as a binary file for read access using the statement
OPEN DATASET dset FOR INPUT IN BINARY MODE.
. If a non-existing file is accessed, an exception that can be handled can be triggered.
Influence of Access Type
Files can be read independently of the access type. Whether data can be read or not depends solely on the position of the file pointer. If the latter is at the end of the file or after the file, no data can be read and sy-subrc will be set to 4.
Influence of the Storage Type
The import function will take place irrespective of the storage type in which the file was opened with the statement OPEN DATASET.
If the file was opened as a text file or as a legacy text file, the data is normally read from the current position of the file pointer to the next end-of-line marking, and the file pointer is positioned after the end-of-line marking. If the data object dobj is too short for the number of read characters, the superfluous characters and bytes are cut off. If it is longer, it will be filled with blanks to the right.
If the file was opened as a binary file or as a legacy-binary file, as much data is read that fits into the data object dobj. If the data object dobj is longer than the number of exported characters, it is filled with hexadecimal 0 on the right.
If the specified storage type makes conversion necessary, this is executed before the assignment to the data object dobj. Afterwards, the read data is placed, byte by byte, into the data object.
System Fields
sy-subrc Meaning
0 Data was read without reaching end of file.
4 Data was read and the end of the file was reached or there was an attempt to read after the end of the file.
Thanks,
Samantak.
<b>Rewards points for useful answers.</b> -
Reading text file in ABAP.
Hi Abapers,
I need to read a text file in application server.
but not total text file.
I need to check each character and only required text is expect in download file.
simply i can say, i need to read a file char by char
is it possible in abap?
Regards
lakshman balanagu.Hi Lakshman,
1.Files in application server are called 'Sequential files'. they are also called as 'Dataset'.
2.To handle sequential file we can follow the below said procedures.
a. To open sequentioal file:
OPEN DATASET <FILENAME> FOR { OUTPUT/INPUT/APPENDING}
IN {TEXT MODE/BINARY MODE}
b. To process ( reading/writing):
TRANSFER <FIELD> TO <FILE NAME>,
READ DATASET <FILENAME> INTO <FIELD>.
c. To close:
CLOSE DATASET <FILENAME>
3. To see the application files tcode: AL11
4. Download the files from application server to presentation server : CG3Y
5. Presentation server to application server : CG3Z
Regards,
Sakthivel.VT -
Read from file, compare string and return a part
Hello, is there a reasonable good way to read text file containing information, and if string comparison is successfull, return a nother part of the string.
What i have done so far:
set file_name to (path to desktop as text) & "filename.txt"
set F to open for access tiedosto with write permission
set file_information to (read F)
close access F
(i will be moving this to Application Support/Appname/ when i figure it out).
The data in the file is in following format
12234567890Name1
98765432123Name2
92347928347Name3
What im stuck now is:
I need to compare each line the 10 numbers with search string, if match is found, the name part should be returned.
Any idea's?You already read in the text file, so you need to put the data thru a repeat loop:
repeat with name_part in paragraphs of file_information
-- ((character 1 thru 10 of name_part) as string) is the first 10 digits of name part
if ((character 1 thru 10 of name_part) as string) is search_string then return name_part
end -
Not recognizing # while reading the file from application server
Hi
I am reading a file from application server. While reading into internal table with read statement the last field in each record is filling with hash symbol in the last digit. If I write any if condition with HASH symbol its not going inside the if condition, means its not recognizing as hash may be its internally treating as some other. I need to remove the hash from that field. How I can do that.
Thanks,
kishoreI faced exact situation. Yes, internally its treated as some other special character. What i did was, becuase hash symbol was always coming at the end...i created a dummy field in my internal table so that it will not interfere with my actual data. When i see the data in my internal table, the hash always falls in the last field (dummy) which i will ignore. I could not get solution to remove this hash so i adopted this approach and it worked!!
Hope it helps,
SKJ -
How to read a file content in a ordinary java program
Please help me is there is any method is available to read the content of the file.I am asking in pure java not in the servlets.Guys please help me.I got structed here
Reading a text file in the systems character encoding: use the class java.io.FileReader. You can wrap a BufferedReader around it to get better performance (and a simple way to read lines)
Reading a text file in another character encoding: use FileInputStream and convert it to a reader with:
InputStreamReader reader = new InputStreamReader(fileStream, encoding);
where encoding is the character encoding as a String (like "Cp850" or "UTF-16LE"). Again, you can wrap a BufferedReader around that reader.
Reading a binary file: open a FileInputStream to the file and just read the bytes from it. You can use BufferedInputStream for better performance and DataInputStream to get a simple way to read primitive types like double and char.
Please help me is there is any method is available to
read the content of the file.I am asking in pure java
not in the servlets.Guys please help me.I got structed
hereAren't servlets pure Java? -
Reading TemSe files in unicode system showing greek letters
Hi Gurus,
I am facing issue with the newly converted unicode (cp 4102) spool files. I am using the standard functions RSTS_OPEN_RLC , RSTS_READ and RSTS_CLOSE to read TemSe files and create output files on application server using OPEN DATASET and TRANSFER.
After conversion to unicode, these function calls are returning some greek characters with size of the itab doubled (and that is due to double-byte UTF-16 charcter set) .
The program generates
Runtime Error CONVT_CODEPAGE
Except. CX_SY_CONVERSION_CODEPAGE
" While a text was being converted from code page '4102' to '1100', on
the following occurred:
- an character was discovered that could not be represented in one o
the two code pages;
- the system established that this conversion is not supported."
the statement
TRANSFER TEMSE_DATA TO ACT_FILE
generates this error.
Can someone tell me what is missing ?
TIA
WasimThe extra characters are pasted below:
㤀 ㈀㈀㐀 ㈀㜀㐀㐀㐀 㤀 ㈀ 㘀 㤀㈀ 圀 㤀㐀 唀⸀匀⸀ 䈀䄀一䬀
These kind of characters are also visible in SP11 contents for spool files.
Again the question: which font to you use to display in your SAPGUI?
The spool files are generated through language EN and should not show any non-english characters.
That doesn´t tell anything. One advantage of Unicode is that you can enter, display and modify all languages/characters with an English logon.
Another question: What technology is generating those spool files? Sapscript? Smartforms? Self written program?
Markus
Maybe you are looking for
-
SQL DTU alerts not working in Azure New Portal
I setup a lot of DTU alerts for our PROD Databases and setup some low threasholds to test the alerts and none of them fired. I even witnessed 3 DBs above my threashold for more a good 30 min but never an email. something broken with the alerts?
-
How do I save an Illustrator CS3 file to a CS2 files so someone with CS2 can open it? Using Mac OS 10.4.11. Thx much.
-
How i use java applet data grid i have many record i need show in grid
hi master sir i have many record i want show all record in applet grid how i use grid in applet please send me sample code of java class how send many record to grid and applet code how use applet grid thanks aamir
-
Why using XI instead of existing custom made interfaces
Hi All, I am trying to convince my company (automotive industry) of using XI for putting up communication between the different applications (SAP and NON-SAP). Currently all interfaces are custom developments on each individual component. Benefits wo
-
Display hard coded dscrption to user instead of data in lov in discoverer
Hi, I have created the LOV using discoverer administrator.. but the value in lov is getting displayed as MIG 1 MIG2 etc.. the ones which are in my column . But i want to display name against these values to user( front end i.e. discoverer plus) like