Results 1 to 7 of 7
- 04-22-2008, 06:58 PM #1
Member
- Join Date
- Apr 2008
- Posts
- 9
- Rep Power
- 0
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());
}
}
}
- 04-22-2008, 07:16 PM #2
Member
- Join Date
- Apr 2008
- Posts
- 28
- Rep Power
- 0
Assume that String
I haven't tested the solution might be wrong.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));
you can just replace some output methods, but the genral idea is above.Last edited by fireball2008; 04-22-2008 at 07:20 PM.
- 04-22-2008, 07:21 PM #3
Member
- Join Date
- Apr 2008
- Posts
- 9
- Rep Power
- 0
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.
- 04-22-2008, 07:23 PM #4
Member
- Join Date
- Apr 2008
- Posts
- 28
- Rep Power
- 0
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
- 04-22-2008, 07:50 PM #5
Member
- Join Date
- Apr 2008
- Posts
- 9
- Rep Power
- 0
elaboration
I am a beginner. and i would appreciate more elaboration.
- 04-23-2008, 01:56 AM #6
Member
- Join Date
- Apr 2008
- Posts
- 28
- Rep Power
- 0
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
- 04-23-2008, 02:47 AM #7
Member
- Join Date
- Apr 2008
- Posts
- 28
- Rep Power
- 0
example of file writerJava 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(); }}
also shows how to create a new line in file
Similar Threads
-
DES algorithm (Read and Write bytes to file)
By JoaoPe in forum Advanced JavaReplies: 6Last Post: 07-29-2008, 03:46 PM -
Read-File Write Display substring
By hiklior in forum New To JavaReplies: 3Last Post: 04-18-2008, 11:45 AM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM -
.jnlp cant read & write on browser despite signed .jar
By bongia in forum New To JavaReplies: 0Last Post: 11-14-2007, 06:04 PM -
to find the workspace path at runtime to write the file
By Gnanam in forum Advanced JavaReplies: 5Last Post: 07-31-2007, 04:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks