Results 1 to 7 of 7
  1. #1
    hiklior is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default 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());
    }
    }
    }

  2. #2
    fireball2008 is offline Member
    Join Date
    Apr 2008
    Posts
    28
    Rep Power
    0

    Default

    Assume that String
    Java 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 07:20 PM.

  3. #3
    hiklior is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default 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.

  4. #4
    fireball2008 is offline Member
    Join Date
    Apr 2008
    Posts
    28
    Rep Power
    0

    Default

    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

  5. #5
    hiklior is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default elaboration

    I am a beginner. and i would appreciate more elaboration.

  6. #6
    fireball2008 is offline Member
    Join Date
    Apr 2008
    Posts
    28
    Rep Power
    0

    Default

    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

  7. #7
    fireball2008 is offline Member
    Join Date
    Apr 2008
    Posts
    28
    Rep Power
    0

    Default

    Java 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

Similar Threads

  1. DES algorithm (Read and Write bytes to file)
    By JoaoPe in forum Advanced Java
    Replies: 6
    Last Post: 07-29-2008, 03:46 PM
  2. Read-File Write Display substring
    By hiklior in forum New To Java
    Replies: 3
    Last Post: 04-18-2008, 11:45 AM
  3. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM
  4. Replies: 0
    Last Post: 11-14-2007, 06:04 PM
  5. Replies: 5
    Last Post: 07-31-2007, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •