amitd
asked on
how to read from file
hi guys what i want to do is to write the stuff like this
# uefwijoo
(
name:Tarzan,
age:24,
Quali:Engineer,
)
# uefwijoo
(
name:Tarzan,
age:24,
Quali:FirstClass
)
so what i am doin is
import java.util.*;
import java.io.*;
public class Transfer
{
private DataInputStream din;
private DataOutputStream dout;
private StringTokenizer tokenizer;
private Vector tokens;
private String comment = "#";
private String path;
private String line = "";
private boolean flag;
private Hashtable ht;
public Transfer(String path)
{
this.path = path;
try
{
File f = new File(path);
din = new DataInputStream(new FileInputStream(f));
dout = new DataOutputStream(new FileOutputStream("OutCome. txt"));
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
private void transfer()
{
try
{
while((line = din.readLine()) != null)
{
line.trim();
if((line.charAt(0)) =='#')
{
flag = true;
String comment = line.substring(1);
dout.writeBytes(line);
}
else
{
flag = false;
if(line.indexOf("(")!= -1)
{
line.substring(1);
}
else if(line.indexOf(")")!= -1)
break;
System.out.println(line);
while(line.length()!=0)
tokenizer = new StringTokenizer(line,"=");
// while(tokenizer.hasMoreTok ens())
// {
String name = tokenizer.nextToken();
System.out.println(name);
String value = tokenizer.nextToken();
System.out.println(value);
for(int i = 0;i<tokens.size();i++)
{
dout.writeBytes(String.val ueOf(token s.elementA t(i)));
}
//}
}
}
System.out.println("hey man it's end of line 1");
}//try
catch(IOException ie)
{
ie.printStackTrace();
}
}
public static void main(String args[])
{
Transfer r = new Transfer("c:"+File.separat or+"ad"+Fi le.separat or+"depmas ks");
r.transfer();
}
}
my problem is this code tokenizes the line for first time only
while loop never loops back again
i mean i should get the o/p as
name
Tarzan
age
24 so on
so wht i get is
name
Tarzan
# uefwijoo
(
name:Tarzan,
age:24,
Quali:Engineer,
)
# uefwijoo
(
name:Tarzan,
age:24,
Quali:FirstClass
)
so what i am doin is
import java.util.*;
import java.io.*;
public class Transfer
{
private DataInputStream din;
private DataOutputStream dout;
private StringTokenizer tokenizer;
private Vector tokens;
private String comment = "#";
private String path;
private String line = "";
private boolean flag;
private Hashtable ht;
public Transfer(String path)
{
this.path = path;
try
{
File f = new File(path);
din = new DataInputStream(new FileInputStream(f));
dout = new DataOutputStream(new FileOutputStream("OutCome.
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
private void transfer()
{
try
{
while((line = din.readLine()) != null)
{
line.trim();
if((line.charAt(0)) =='#')
{
flag = true;
String comment = line.substring(1);
dout.writeBytes(line);
}
else
{
flag = false;
if(line.indexOf("(")!= -1)
{
line.substring(1);
}
else if(line.indexOf(")")!= -1)
break;
System.out.println(line);
while(line.length()!=0)
tokenizer = new StringTokenizer(line,"=");
// while(tokenizer.hasMoreTok
// {
String name = tokenizer.nextToken();
System.out.println(name);
String value = tokenizer.nextToken();
System.out.println(value);
for(int i = 0;i<tokens.size();i++)
{
dout.writeBytes(String.val
}
//}
}
}
System.out.println("hey man it's end of line 1");
}//try
catch(IOException ie)
{
ie.printStackTrace();
}
}
public static void main(String args[])
{
Transfer r = new Transfer("c:"+File.separat
r.transfer();
}
}
my problem is this code tokenizes the line for first time only
while loop never loops back again
i mean i should get the o/p as
name
Tarzan
age
24 so on
so wht i get is
name
Tarzan
try this:
import java.util.*;
import java.io.*;
public class Transfer {
private DataInputStream din;
private DataOutputStream dout;
private StringTokenizer tokenizer;
private Vector tokens;
private String comment = "#";
private String path;
private String line = "";
private boolean flag;
private Hashtable ht;
public Transfer(String path) {
this.path = path;
try {
File f = new File(path);
din = new DataInputStream(new FileInputStream(f));
dout = new DataOutputStream(new FileOutputStream("c:/tmp/O utCome.txt "));
tokens=new Vector();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public static void main(String args[]) {
Transfer r = new Transfer("c:/tmp/output.tx t");
r.transfer();
}
private void transfer() {
int count = 0;
try {
while ((line = din.readLine()) != null) {
count++;
line = line.trim();
if ((line.charAt(0)) == '#') {
line = "";
//flag = true;
//String comment = line.substring(1);
//dout.writeBytes(line);
} else {
flag = false;
if (line.indexOf("(") != -1) {
//whats the purpose of this?
//line = line.substring(1);
//line.substring(1);
line = "";
} else
if (line.indexOf(")") != -1) {
//whats the purpose of this?
//break;
line = "";
}
while (line.length() != 0) {
tokenizer = new StringTokenizer(line, ":");
// while(tokenizer.hasMoreTok ens())
// {
String name = tokenizer.nextToken();
tokens.addElement(name);
String value = tokenizer.nextToken();
tokens.addElement(value);
for (int i = 0; i < tokens.size(); i++) {
dout.writeBytes((String) tokens.elementAt(i));
dout.writeBytes("\n");
}
tokens.setSize(0);
line = "";
}
System.out.println("hey man it's end of line " + count);
}
}
} //try
catch (IOException ie) {
ie.printStackTrace();
}
}
}
Dont understand all of what you are trying to do with the commas but your output file will appear as you stipulated
Blair
import java.util.*;
import java.io.*;
public class Transfer {
private DataInputStream din;
private DataOutputStream dout;
private StringTokenizer tokenizer;
private Vector tokens;
private String comment = "#";
private String path;
private String line = "";
private boolean flag;
private Hashtable ht;
public Transfer(String path) {
this.path = path;
try {
File f = new File(path);
din = new DataInputStream(new FileInputStream(f));
dout = new DataOutputStream(new FileOutputStream("c:/tmp/O
tokens=new Vector();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public static void main(String args[]) {
Transfer r = new Transfer("c:/tmp/output.tx
r.transfer();
}
private void transfer() {
int count = 0;
try {
while ((line = din.readLine()) != null) {
count++;
line = line.trim();
if ((line.charAt(0)) == '#') {
line = "";
//flag = true;
//String comment = line.substring(1);
//dout.writeBytes(line);
} else {
flag = false;
if (line.indexOf("(") != -1) {
//whats the purpose of this?
//line = line.substring(1);
//line.substring(1);
line = "";
} else
if (line.indexOf(")") != -1) {
//whats the purpose of this?
//break;
line = "";
}
while (line.length() != 0) {
tokenizer = new StringTokenizer(line, ":");
// while(tokenizer.hasMoreTok
// {
String name = tokenizer.nextToken();
tokens.addElement(name);
String value = tokenizer.nextToken();
tokens.addElement(value);
for (int i = 0; i < tokens.size(); i++) {
dout.writeBytes((String) tokens.elementAt(i));
dout.writeBytes("\n");
}
tokens.setSize(0);
line = "";
}
System.out.println("hey man it's end of line " + count);
}
}
} //try
catch (IOException ie) {
ie.printStackTrace();
}
}
}
Dont understand all of what you are trying to do with the commas but your output file will appear as you stipulated
Blair
i've done another version and put it up as www.cowfish.org.uk/download/BillTransfer.java
its a bit smaller than yours and gets rid of the bits that i don't understand, which may very well be necessary for your final implementation
you will have to change the file path before running it though...
its a bit smaller than yours and gets rid of the bits that i don't understand, which may very well be necessary for your final implementation
you will have to change the file path before running it though...
hello friends what both of u have added is only output statement
and haven't change any thing elase
as per me our friend don't want that
he may not be able to tokanize all string that might be his problem i suppose
well amitd i am working on ur code
get result soon
and haven't change any thing elase
as per me our friend don't want that
he may not be able to tokanize all string that might be his problem i suppose
well amitd i am working on ur code
get result soon
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hi
billy i tried to access the url given by u but it says program not on server
it gives me 404NotFound Error
billy i tried to access the url given by u but it says program not on server
it gives me 404NotFound Error
ASKER
hi rjack man
what u have suggested is right
it works for me
Thanx to all of u try to help me
AMIT
what u have suggested is right
it works for me
Thanx to all of u try to help me
AMIT
while(line.length()!=0)
tokenizer = new StringTokenizer(line,"=");
i have fixed your code so that it outputs what i think you want, and you can get it from www.cowfish.org.uk/download/Transfer.java
tell me if i've got the right idea.