| | |
how to write in a file
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jul 2007
Posts: 36
Reputation:
Solved Threads: 0
How to write these file datas in the new file...It should be done by wrting bytes into characters
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class fileread
{
public static void main(String[] args)
{
File file = new File("E:\\ir_Proj
estimate_out.txt");
byte[] b = new byte[(int) file.length()];
try {
FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read(b);
for (int i = 0; i < b.length; i++)
{
System.out.println((char)b);
}
} catch (FileNotFoundException e) {
System.out.println("File Not Found.");
e.printStackTrace();
}
catch (IOException e1)
{
System.out.println("Error Reading The File.");
e1.printStackTrace();
}
}
}
thank u
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class fileread
{
public static void main(String[] args)
{
File file = new File("E:\\ir_Proj
estimate_out.txt");
byte[] b = new byte[(int) file.length()];
try {
FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read(b);
for (int i = 0; i < b.length; i++)
{
System.out.println((char)b);
}
} catch (FileNotFoundException e) {
System.out.println("File Not Found.");
e.printStackTrace();
}
catch (IOException e1)
{
System.out.println("Error Reading The File.");
e1.printStackTrace();
}
}
}
thank u
Well, your question barely makes sense given the code that you posted (which needs code tags too). Maybe this example of writing to a file will help http://www.exampledepot.com/egs/java...iteToFile.html
•
•
Join Date: Jul 2007
Posts: 36
Reputation:
Solved Threads: 0
Actually i want to process tat text files, which means am removing sum line in between the paragraphs so i want the correct order to be as my output.As u told if i read in strings and writing in file, means the format is different. All the lines are printing in same line.
Last edited by abar_sow; Sep 14th, 2007 at 4:48 pm.
•
•
Join Date: Jul 2007
Posts: 36
Reputation:
Solved Threads: 0
I solved My prob..
Java Syntax (Toggle Plain Text)
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.FilenameFilter; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public void removeLine(String filename)throws IOException{ BufferedReader br = new BufferedReader(new FileReader("D:\\TextFiles\\" + filename)); FileWriter f2=new FileWriter("D:\\TextFiles\\Output\\" + filename,true); String s; //StringBuffer sb; /*Boolean found;*/ while((s=br.readLine())!= null ){ try{ System.out.println(s); Pattern p = Pattern.compile("(.*)[Ff]ig.?\\s*\\d*\\.\\d*(.*)"); Matcher m = p.matcher (s); /*found=m.matches ();*/ if(m.find()==true){ continue; } s = s + "\r\n"; f2.write(s); }catch (Exception e){ e.printStackTrace(); } } br.close(); f2.close(); } public static void main(String args[])throws IOException { Main main = new Main(); File file = new File("D:\\TextFiles"); FilenameFilter filter = new Onlyext("TXT"); String files[] = file.list(filter); if(files!=null){ for (int i = 0; i < files.length; i++) { //System.out.println(files[i]); main.removeLine(files[i].toString()); } } } } class Onlyext implements FilenameFilter { String ext; public Onlyext(String ext) { this.ext = "." + ext; } public boolean accept(File dir, String name) { return name.endsWith(ext); } }
![]() |
Similar Threads
- Write to a file in C++ (C++)
- Read/write to same file > once, Help (C++)
- write in file and screen? (C++)
- Write to a file (C)
- line up JTextField GUI java & write to file (Java)
Other Threads in the Java Forum
- Previous Thread: please help me to build a java project.
- Next Thread: Do desktop applications rely on database software?
Views: 9095 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code color compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool keyword linux list loop map method methods mobile netbeans newbie number object oracle pong print problem producer program programming project projectideas read recursion reflection rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






