Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-22-2008, 07:58 PM
Member
 
Join Date: Apr 2008
Posts: 9
hiklior is on a distinguished road
Read/Find Substring/Write to new file
Hi guys,

i would appreciate your help on this . I am trying to create a little program for myself. I needed a program to read a file and find 0000 (4 zeros) from that file and give me a number before each 4 zeros along with the 4 zeros and write it in a different file for me but each of the number in this new file should be in different line.

example:

10000 2121000044540000 0012000

Output
10000
10000
40000


This is what i have until now and i am stuck.

Please help

import java.io.*;
class FileReadTest
{
public static void main(String args[])
{
try{

FileInputStream fstream = new FileInputStream("c:/MyFile.txt");

DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;


FileWriter fstream1 = new FileWriter("c:/out.txt");
BufferedWriter out = new BufferedWriter(fstream1);

while ((strLine = br.readLine()) != null)
{
System.out.println (strLine);
out.write(strLine);
}

in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-22-2008, 08:16 PM
Member
 
Join Date: Apr 2008
Posts: 28
fireball2008 is on a distinguished road
Send a message via AIM to fireball2008
Assume that String
Code:
FourZero ="0000"; String num = some String from input; int difference = num.length()-FourZero.length(); for(int x=1; x<=difference; x++) if(FourZero.equals(num.subString(x,x+4))); System.out.println(num.subString(x-1,x+4));
I haven't tested the solution might be wrong.
you can just replace some output methods, but the genral idea is above.

Last edited by fireball2008 : 04-22-2008 at 08:20 PM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-22-2008, 08:21 PM
Member
 
Join Date: Apr 2008
Posts: 9
hiklior is on a distinguished road
Issue
I actually need this program to read from a file . Find substring as i mentioned above and write it in a different file with new line for each substring that it finds.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-22-2008, 08:23 PM
Member
 
Join Date: Apr 2008
Posts: 28
fireball2008 is on a distinguished road
Send a message via AIM to fireball2008
you can use the scanner class to read from a file, and use the method above find the subString and write to the file line by line
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-22-2008, 08:50 PM
Member
 
Join Date: Apr 2008
Posts: 9
hiklior is on a distinguished road
elaboration
I am a beginner. and i would appreciate more elaboration.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-23-2008, 02:56 AM
Member
 
Join Date: Apr 2008
Posts: 28
fireball2008 is on a distinguished road
Send a message via AIM to fireball2008
Quote:
Originally Posted by hiklior View Post
I am a beginner. and i would appreciate more elaboration.
Scanner (Java 2 Platform SE 5.0)
here is the jave Scanner class
to use it you have to import java.util.Scanner;
they also give u example how to use it
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-23-2008, 03:47 AM
Member
 
Join Date: Apr 2008
Posts: 28
fireball2008 is on a distinguished road
Send a message via AIM to fireball2008
Code:
import java.io.*; import java.util.*; import java.lang.Integer; import java.io.PrintWriter; import java.io.OutputStreamWriter; public class fileWriter { public void ok()throws IOException { BufferedWriter out = new BufferedWriter(new FileWriter("output")); out.write("aString"); out.newLine(); // create a new line in file out.write("ok"); out.close(); } public static void main(String args[])throws IOException {fileWriter f = new fileWriter(); f.ok(); }}
example of file writer
also shows how to create a new line in file
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
DES algorithm (Read and Write bytes to file) JoaoPe Advanced Java 6 07-29-2008 04:46 PM
Read-File Write Display substring hiklior New To Java 3 04-18-2008 12:45 PM
How to read a text file from a Java Archive File Java Tip Java Tips 0 02-08-2008 10:13 AM
.jnlp cant read & write on browser despite signed .jar bongia New To Java 0 11-14-2007 07:04 PM
to find the workspace path at runtime to write the file Gnanam Advanced Java 5 07-31-2007 05:22 PM


All times are GMT +3. The time now is 09:16 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org