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

Recommended Answers

All 5 Replies

I already referred wat t link u gave .But still it didnt clear out my doubt.In the previous prog i red a file and i converted tat into byte.If i want to write again tat converted byte into another file how t code shd come??Do u got wat i mean

Member Avatar for iamthwee

Why are you reading it as bytes.Read and write it as strings you hear me!

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.

I solved My prob..

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);
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.