Extract a part from a txt file
Hi, I've a simple problem which unfortunately can not solve.
I have some txt's that I want only to extract a part o them. (The first 100 characters). Here is my code:
import java.io.*;
public class ReadSource {
public static void main(String[] arguments) {
try {
FileReader file = new FileReader("1.txt");
BufferedReader buff = new BufferedReader(file);
boolean eof = false;
while (!eof) {
String line = buff.readLine();
if (line == null)
eof = true;
else{
int start = line.indexOf("something");
int end = line.indexOf("something_else");
String str2 = line.substring(start, end);
System.out.println(str2);
buff.close();
} catch (IOException e) {
System.out.println("Error -- " + e.toString());
}and the error tha java displays is:
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1438)
at ReadSource.main(ReadSource.java:21)
Exception in thread "main"
Tim I'm very sorry but is still don't work. It still display the same error.
Look at my code
import java.io.*;
public class ReadSource {
public static void main(String[] arguments) {
try {
BufferedReader reader = new BufferedReader(new FileReader("1.txt"));
BufferedWriter writer = new BufferedWriter(new StringWriter());
String line = null;
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
String content = writer.toString();
// Now you have the whole file content into the content String
int start = content.indexOf("smth");
int end = content.indexOf("smth2");
String str2 = content.substring(start,end);
System.out.println("start = "+start+", end = "+end);
System.out.println(str2);
reader.close();
}catch (IOException e) {
System.out.println("Error -- " + e.toString());
}It looks very logical, I don't know what happens
Thank you very much for your help.
Kostas
Similar Messages
-
How to use automator to extract specific text from json txt file
I'm trying to set up an Automator folder action to extract certain data from json files. I'm pulling metadata from YouTube videos, and I want to extract the Title of the video, the URL for the video, and the date uploaded.
Sample json data excerpts:
"upload_date": "20130319"
"title": "[title of varying length]"
"webpage_url": "https://www.youtube.com/watch?v=[video id]"
Based on this thread, seems I should be able to have Automator (or any means of using a shell script) find data and extract it into a .txt file, which I can then open as a space delimited file in Excel or Numbers. That answer assumes a static number of digits for the text to be extracted, though. Is there a way Automator can search through the json file and extract the text - however long - after "title" and "webpage_url"?
json files are all in the same folder, and all end in .info.json.
Any help greatly appreciated!Hello
You might try the following perl script, which will process every *.json file in current directory and yield out.csv.
* CSV currently uses space for field separator as you requested. Note that Numbers.app cannot import such CSV file correctly.
#!/bin/bash
/usr/bin/perl -CSDA -w <<'EOF' - *.json > out.csv
use strict;
use JSON::Syck;
$JSON::Syck::ImplicitUnicode = 1;
# json node paths to extract
my @paths = ('/upload_date', '/title', '/webpage_url');
for (@ARGV) {
my $json;
open(IN, "<", $_) or die "$!";
local $/;
$json = <IN>;
close IN;
my $data = JSON::Syck::Load($json) or next;
my @values = map { &json_node_at_path($data, $_) } @paths;
# output CSV spec
# - field separator = SPACE
# - record separator = LF
# - every field is quoted
local $, = qq( );
local $\ = qq(\n);
print map { s/"/""/og; q(").$_.q("); } @values;
sub json_node_at_path ($$) {
# $ : (reference) json object
# $ : (string) node path
# E.g. Given node path = '/abc/0/def', it returns either
# $obj->{'abc'}->[0]->{'def'} if $obj->{'abc'} is ARRAY; or
# $obj->{'abc'}->{'0'}->{'def'} if $obj->{'abc'} is HASH.
my ($obj, $path) = @_;
my $r = $obj;
for ( map { /(^.+$)/ } split /\//, $path ) {
if ( /^[0-9]+$/ && ref($r) eq 'ARRAY' ) {
$r = $r->[$_];
else {
$r = $r->{$_};
return $r;
EOF
For Automator workflow, you may use Run Shell Script action as follows, which will receive json files and yield out_YYYY-MM-DD_HHMMSS.csv on desktop.
Run Shell Script action
- Shell = /bin/bash
- Pass input = as arguments
- Code = as follows
#!/bin/bash
/usr/bin/perl -CSDA -w <<'EOF' - "$@" > ~/Desktop/out_"$(date '+%F_%H%M%S')".csv
use strict;
use JSON::Syck;
$JSON::Syck::ImplicitUnicode = 1;
# json node paths to extract
my @paths = ('/upload_date', '/title', '/webpage_url');
for (@ARGV) {
my $json;
open(IN, "<", $_) or die "$!";
local $/;
$json = <IN>;
close IN;
my $data = JSON::Syck::Load($json) or next;
my @values = map { &json_node_at_path($data, $_) } @paths;
# output CSV spec
# - field separator = SPACE
# - record separator = LF
# - every field is quoted
local $, = qq( );
local $\ = qq(\n);
print map { s/"/""/og; q(").$_.q("); } @values;
sub json_node_at_path ($$) {
# $ : (reference) json object
# $ : (string) node path
# E.g. Given node path = '/abc/0/def', it returns either
# $obj->{'abc'}->[0]->{'def'} if $obj->{'abc'} is ARRAY; or
# $obj->{'abc'}->{'0'}->{'def'} if $obj->{'abc'} is HASH.
my ($obj, $path) = @_;
my $r = $obj;
for ( map { /(^.+$)/ } split /\//, $path ) {
if ( /^[0-9]+$/ && ref($r) eq 'ARRAY' ) {
$r = $r->[$_];
else {
$r = $r->{$_};
return $r;
EOF
Tested under OS X 10.6.8.
Hope this may help,
H -
I have a VI and an attched .txt data file. Now I want to read the data from the .txt file and display it as an array in the front panel. But the result is not right. Any help?
Attachments:
try2.txt 2 KB
read_array.vi 21 KBThe problem is in the delimiters in your text file. By default, Read From Spreadsheet File.vi expects a tab delimited file. You can specify a delimiter (like a space), but Read From Spreadsheet File.vi has a problem with repeated delimiters: if you specify a single space as a delimiter and Read From Spreadsheet File.vi finds two spaces back-to-back, it stops reading that line. Your file (as I got it from your earlier post) is delimited by 4 spaces.
Here are some of your choices to fix your problem.
1. Change the source file to a tab delimited file. Your VI will then run as is.
2. Change the source file to be delimited by a single space (rather than 4), then wire a string constant containing one space to the delimiter input of Read From Spreadsheet File.vi.
3. Wire a string constant containing 4 spaces to the delimiter input of Read From Spreadsheet File.vi. Then your text file will run as is.
Depending on where your text file comes from (see more comments below), I'd vote for choice 1: a tab delimited text file. It's the most common text output of spreadsheet programs.
Comments for choices 1 and 2: Where does the text file come from? Is it automatically generated or manually generated? Will it be generated multiple times or just once? If it's manually generated or generated just once, you can use any text editor to change 4 spaces to a tab or to a single space. Note: if you want to change it to a tab delimited file, you can't enter a tab directly into a box in the search & replace dialog of many programs like notepad, but you can do a cut and paste. Before you start your search and replace (just in the text window of the editor), press tab. A tab character will be entered. Press Shift-LeftArrow (not Backspace) to highlight the tab character. Press Ctrl-X to cut the tab character. Start your search and replace (Ctrl-H in notepad in Windows 2000). Click into the Find What box. Enter four spaces. Click into the Replace With box. Press Ctrl-V to paste the tab character. And another thing: older versions of notepad don't have search and replace. Use any editor or word processor that does. -
Problem on reading and writing from from a *.txt file
I get Problem on reading and writing from from a *.txt file. The following is the read() method...
The software said the DataInputStream is depreciated. Can anyone help me please?
public void read()
File file = new File("C://Documents and Settings//Charles//My Documents//Brunel//EE2065//Assignment and Lab//Assignment 4 and Lab 4//data.txt");
FileInputStream in = null;
String str = "";
try
in = new BufferedReader(file);
//in = new FileInputStream(file);
for(;;)
str = new BufferedReader(in).readLine();
//str = new DataInputStream(in).readLine();
if(str == null)
break;
System.out.print(str);
in.close();
catch(IOException e)
System.err.println("execution error: " +e);
}Thank you for your reply. I have made some change. However, there is an incompetable type found error.
in = new BufferedReader(new InputStreamReader(in));The following are all of the code.
public void read()
File file = new File("C://Documents and Settings//Charles//My Documents//Brunel//EE2065//Assignment and Lab//Assignment 4 and Lab 4//data.txt");
FileInputStream in = null;
//BufferedReader in = null;
String str = "";
try
in = new BufferedReader(new InputStreamReader(in));
//in = new FileInputStream(file);
for(;;)
BufferedReader Bstr = new BufferedReader(new InputStreamReader(in));
//str = new BufferedReader(in).readLine();
//str = new DataInputStream(in).readLine();
if(str == null)
break;
System.out.print(str);
in.close();
catch(IOException e)
System.err.println("execution error: " +e); -
How to retrieve IndividualStrings from a txt file using String Tokenizer.
hello can any one help me to retrieve the individual strings from a txt file using string tokenizer or some thing like that.
the data in my txt file looks like this way.
Data1;
abc; cder; efu; frg;
abc1; cder2; efu3; frg4;
Data2
sdfabc; sdfcder; hvhefu; fgfrg;
uhfhabc; gffjcder; yugefu; hhfufrg;
Data3
val1; val2; val3; val4; val5; val6;
val1; val2; val3; val4; val5; val6;
val1; val2; val3; val4; val5; val6;
val1; val2; val3; val4; val5; val6;
i need to read the data as an individual strings and i need to pass those values to diffarent labels,the dat in Data3 i have to read those values and add to an table datamodel as 6 columns and rows depends on the data.
i try to retrieve data using buffered reader and inputstream reader,but only the way i am retrieving data as an big string of entire line ,i tried with stringtokenizer but some how i was failed to retrive the data in a way i want,any help would be appreciated.
Regards,Hmmm... looks like the file format isn't even very consistent... why the semicolon after Data1 but not after Data2 or Data3??
Your algorithm is reading character-by-character, and most of the time it's easier to let a StringTokenizer or StreamTokenizer do the work of lexical analysis and let you focus on the parsing.
I am also going to assume your format is very rigid. E.g. section Data1 will ALWAYS come before section Data2, which will come before section Data3, etc... and you might even make the assumption there can never be a Data4, 5, 6, etc... (this is why its nice to have some exact specification, like a grammar, so you know exactly what is and is not allowed.) I will also assume that the section names will always be the same, namely "DataX" where X is a decimal digit.
I tend to like to use StreamTokenizer for this sort of thing, but the additional power and flexibility it gives comes at the price of a steeper learning curve (and it's a little buggy too). So I will ignore this class and focus on StringTokenizer.
I would suggest something like this general framework:
//make a BufferedReader up here...
do
String line = myBufferedReader.readLine();
if (line!=null && line.trim().length()>0)
line = line.trim();
//do some processing on the line
while (line!=null);So what processing to do inside the if statement?
Well, you can recognize the DataX lines easily enough - just do something like a line.startsWith("Data") and check that the last char is a digit... you can even ignore the digit if you know the sections come in a certain order (simplifying assumptions can simplify the code).
Once you figure out which section you're in, you can parse the succeeding lines appropriately. You might instantiate a StringTokenizer, i.e. StringTokenizer strtok = new StringTokenizer(line, ";, "); and then read out the tokens into some Collection, based on the section #. E.g.
strtok = new StringTokenizer(line, ";, ");
if (sectionNo==0)
//read the tokens into the Labels1 collection
else if (sectionNo==1)
//read the tokens into the Labels2 collection
else //sectionNo must be 2
//create a new line in your table model and populate it with the token values...
}I don't think the delimiters are necessary if you are using end-of-line's as delimiters (which is implicit in the fact that you are reading the text out line-by-line). So the original file format you listed looks fine (except you might want to get rid of that rogue semicolon).
Good luck. -
Populate a table reading the data from a TXT file
how can I populate a table reading the data from a TXT file?
thanksHey Kevin!
Using FORMS.TEXT_IO to bulk load data from a file strikes me as re-inventing the wheel. It is just about justifiable in a self-service environment, but I regard the EXTERNAL TABLE is a better solution for that situation as well.
The same applies to UTL_FILE. I think the ability to read text with UTL_FILE is primarily intended for read file-based configuration or file manipulation/processing rather than data loading.
Re-writing a text file into SQL statements is too much like hard work (even with an editor that supports macro definition and regular expressions) for no real benefit. You lose all the bulk load peformance you would get from SQL*Loader. But for QAD I'd probably let you off with it.
You missed out one obvious alternative: using Java to turn the contents of an XML file into a CLOB and inserting it into a table which is read by a PL/SQL procedure that parses the XML records and insert the retrieved data into a table.
Stay lucky, APC -
How can I use Automator to extract specific Data from a text file?
I have several hundred text files that contain a bunch of information. I only need six values from each file and ideally I need them as columns in an excel file.
How can I use Automator to extract specific Data from the text files and either create a new text file or excel file with the info? I have looked all over but can't find a solution. If anyone could please help I would be eternally grateful!!! If there is another, better solution than automator, please let me know!
Example of File Contents:
Link Time =
DD/MMM/YYYY
Random
Text
161 179
bytes of CODE memory (+ 68 range fill )
16 789
bytes of DATA memory (+ 59 absolute )
1 875
bytes of XDATA memory (+ 1 855 absolute )
90 783
bytes of FARCODE memory
What I would like to have as a final file:
EXCEL COLUMN1
Column 2
Column3
Column4
Column5
Column6
MM/DD/YYYY
filename1
161179
16789
1875
90783
MM/DD/YYYY
filename2
xxxxxx
xxxxx
xxxx
xxxxx
MM/DD/YYYY
filename3
xxxxxx
xxxxx
xxxx
xxxxx
Is this possible? I can't imagine having to go through each and every file one by one. Please help!!!Hello
You may try the following AppleScript script. It will ask you to choose a root folder where to start searching for *.map files and then create a CSV file named "out.csv" on desktop which you may import to Excel.
set f to (choose folder with prompt "Choose the root folder to start searching")'s POSIX path
if f ends with "/" then set f to f's text 1 thru -2
do shell script "/usr/bin/perl -CSDA -w <<'EOF' - " & f's quoted form & " > ~/Desktop/out.csv
use strict;
use open IN => ':crlf';
chdir $ARGV[0] or die qq($!);
local $/ = qq(\\0);
my @ff = map {chomp; $_} qx(find . -type f -iname '*.map' -print0);
local $/ = qq(\\n);
# CSV spec
# - record separator is CRLF
# - field separator is comma
# - every field is quoted
# - text encoding is UTF-8
local $\\ = qq(\\015\\012); # CRLF
local $, = qq(,); # COMMA
# print column header row
my @dd = ('column 1', 'column 2', 'column 3', 'column 4', 'column 5', 'column 6');
print map { s/\"/\"\"/og; qq(\").$_.qq(\"); } @dd;
# print data row per each file
while (@ff) {
my $f = shift @ff; # file path
if ( ! open(IN, '<', $f) ) {
warn qq(Failed to open $f: $!);
next;
$f =~ s%^.*/%%og; # file name
@dd = ('', $f, '', '', '', '');
while (<IN>) {
chomp;
$dd[0] = \"$2/$1/$3\" if m%Link Time\\s+=\\s+([0-9]{2})/([0-9]{2})/([0-9]{4})%o;
($dd[2] = $1) =~ s/ //g if m/([0-9 ]+)\\s+bytes of CODE\\s/o;
($dd[3] = $1) =~ s/ //g if m/([0-9 ]+)\\s+bytes of DATA\\s/o;
($dd[4] = $1) =~ s/ //g if m/([0-9 ]+)\\s+bytes of XDATA\\s/o;
($dd[5] = $1) =~ s/ //g if m/([0-9 ]+)\\s+bytes of FARCODE\\s/o;
last unless grep { /^$/ } @dd;
close IN;
print map { s/\"/\"\"/og; qq(\").$_.qq(\"); } @dd;
EOF
Hope this may help,
H -
Which Version of Adobe do I need to be able to "extract" a page from a existing file and save/download to another file?
Acrobat Pro or Standard.
-
How to calculate the number of vowels from a txt file. Pls give me a hand!!
Guys! How do i calculate the number of vowels from a txt file? I have to create a program to count the number of words, sentence, and vowels. So far I had managed to count the number of words. Now I need help on counting sentence and vowels! Pls give me a hand guys!
I first have to read from the file not the string.I
know how to read from a file. Now the problem ishow
to compare a character from a txt file!You fail to understand, that you have read the data
from the file into a string. Now loop over all
characters in the string. Forget about the file.
KajOr forget the scanners just as others have told you. Just read the file character by character using a FileReader.
Kaj -
Reading integers from a .txt file and computing means
I have a program that uses for loops to produce ten integers (1-10) are returns the average mean, geometric mean, and harmonic mean.
Now i need to change this program to read integers from a .txt file and return the same data. Basically i need to change my for statements that are incrementing to read the text file.
Thanks guys.You haven't asked a question. You haven't posted code. What are you expecting here?
But I guess I'll take a stab at it and say you should look at the Scanner class
http://java.sun.com/javase/6/docs/api/java/util/Scanner.html
or BufferedReader
http://java.sun.com/javase/6/docs/api/java/io/BufferedReader.html
I'll never understand what makes people come to the forums, create an account, and ask a lazy question, when they could get a great answer much quicker through Google.
http://www.google.com/search?q=java+file+input (hint: look at the second search result) -
Read numbers from a .txt file and display them in a graph
How can I get Labview 7 to read from a txt. file containing a lot of
coloumns with different datas? There`s only two of the coloumns that are
interesting to me, the first, that contains the time of the measuring, and
one in the middle, that contains the measured temperatures. I want Labview
to read this datas and display them graphicly.
Thanks from StaleHere's one way.
You can also use the help-> find examples and search for "text".
2006 Ultimate LabVIEW G-eek.
Attachments:
Graph.vi 21 KB -
How to fill a table with data from columns & rows from a txt file
I am trying to fill say:
Myname Myclass Mycollege
Mohan MD Stanford
from a txt file into 3 columns of a table. I can easily do it into a textarea component by
clicking a button:
try{
FileReader fr = new FileReader("F:/CreatorProjects1/stan.txt");
BufferedReader br = new BufferedReader(fr);
String s1="";
String s="";
stantxtarea.setValue(s);
while((s=br.readLine())!=null) {
s1 = s1+s;
s1=s1+"\n";
stantxtarea.setValue(s1);
br.close();
fr.close();
} catch(Exception e) {
e.printStackTrace();
return null;
All table components seem to need a database. I do not use any database. I run exe files that give output to txt files. How to solve it?
I appreciate any help in this regard...
dr.mohan raothere may be another way. This is some thing some thing I can recommend. You can create property type of ObjectListDataProvider in session or application based on the scope you required. Create web bean class with property you want to show in the column (say col1, col2, col2). Initialize the list property like similar below
List dataList = new ArrayList();
ObjectListDataProvider mytableprops = new ObjectListDataProvider();
try {
//start loop read data from your txt file
//create instance of web bean for each line
//add web bean instance into dataList
//end of loop readind data from txt file
oldpins.setList(dataList );
oldpins.setObjectType(NotificationProfileBean.class);
} catch (Exception ee1){;}
return oldpins;
Sri Thuraisamy -
What is the 'quickest' way to read char data from a txt file
Hello,
What is the 'quickest' way to read character data from a txt file stored on the phone to be displayed into the screen?
RegardsTo be even a bit more constructive...
Since J2me does not have a BufferedInputStream, it will help to implement it yourself. It's much faster since you read large blocks at ones in stread of seperate chars.
something line this lets you read lines very fast:
while ( bytesread < filesize ) {
length = configfile.read( buff, 0, buff.length );
// append buffer to temp String
if ( length < buff.length ) {
byte[] buf = new byte[length];
System.arraycopy( buff, 0, buf, 0, length );
tmp.append( new String( buf ) );
} else {
tmp.append( new String( buff ) );
// look in tmp string for \r\n
idx1 = tmp.toString().indexOf( "\r\n" );
while ( idx1 >= 0 ) {
//if found, split into line and rest of tmp
line = tmp.toString().substring( 0, idx1 );
/// ... do with it whatever you want ... ////
tmp = new StringBuffer( tmp.toString().substring( idx1 + 2 ) );
idx1 = tmp.toString().indexOf( "\r\n" );
bytesread += length;
} -
Hi Gurs...
Plx help
How to get the count - number of records from a .TXT file through Oracle 8i
EMP.TXT
EMPNO ENAME
1 REENA
2 SUGU
3 RAJ
Count(*)=3
How to get this output in Oracle 8i.
Thanks in advance
Gitasql>create or replace directory TEST_DIR as 'd:\test';
Directory created.
sql>
CREATE OR REPLACE PROCEDURE rd_file
AS
v varchar2(500);
abc number;
in_file utl_file.file_type;
BEGIN
in_file :=utl_file.fopen('TEST_DIR','sample.txt','R');
abc := 0;
loop
utl_file.get_line(in_file,v);
abc := abc + 1;
end loop;
EXCEPTION
when no_data_found then
utl_file.fclose(in_file);
dbms_output.put_line(abc);
END ;
show errors;
Procedure created.
No errors
sql>
begin
rd_file;
end;
4
PL/SQL procedure successfully completed
Message was edited by:
jeneesh -
Importing accents from a txt file
Hi everyone
I have the following proble that I can't solve for now:
I've got a pdf file with forms
I've got an excel file that I export to txt (tab delimited text file)
When I import the data from the txt file to the pdf everything is fine but the french accents (é or è or à). For example the "é" becomes a " ,".
I thinks it might be related to unicode or utf but I don't know how to export properly then import those characters.
Any idea ?
Thanks
Florent
PS: I'm using Excell 2011 on MAC and Acrobat 9 Pro.Done that already, no luck.
I've saved in all the txt formats possible in my Excel.
Maybe you are looking for
-
How do I load a playlist onto my 4th gen shuffle using itunes 11?
I have a 4th generation shuffle, and just installed itunes 11. It's ludicrous that the shuffle doesn't allow you to reorder your music (or in my case audiobooks) manually, or even to resort by album/disc/track #. Even so, at least in iTunes 10 you c
-
If your ipod shuffle won't respond read this it might help
Hi I got this from the apple help line. It fixed my ipod shuffle. I hope it helps. My ipod had music on it and was recognized by the computer and itunes, it just wouldn't play anything and the green light just stayed on. I was using windows xp If tha
-
Hi am trying to create a linked list out of an object i have called ClassID. I have a Link class (that creates a node correct?): import java.io.*; public class Link public ClassID id; public Link next; public Link(ClassID inID)
-
HP7160 photo printer stopped working after 8.1 update. Just stays in queue
I do not know what bit it is. I have a pavilion 20 HP all in one. It seems after the latest update nothing is working. application and printer.
-
Is there a virtual pc or Windows emulator for iMac intel duo?
Hi there, I just got a new iMac intel duo last week and its fantastic. Unfornatuly I need to run a stock market program that only runs on windows... This is the only program that i need to run on the windows platform. I was told to get the intel duo