Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java I/O

Status
Not open for further replies.

kvngrt

IS-IT--Management
Aug 20, 2002
1
0
0
US
I have the bulk of the program written, but my problem is that I have some file that has 3 leading blank lines before the text begins. I need to remove those blank lines or skip the lines in the beginning of the file and just copy the remaining text to a new file. I can copy the file and create a new one, but it always has the leading whitespace and blank lines before the text. I tried skipBytes() but it is inefficient. Sample code would help.
Thanks in advance.

here's the code:

//Create a file chooser
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(convert3.this);



if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();

try {




DataInputStream inStream= new DataInputStream(new FileInputStream(file));
(new FileInputStream(file));
DataOutputStream outStream = new DataOutputStream(new FileOutputStream( "c:/note1.txt",true));




//long size = file.length(); // Get size of file.

int c;

while ((c = inStream.read()) != -1)


{


outStream.write(c);

}


inStream.close();
outStream.close();

} catch ( java.io.IOException exception) {
if ( exception instanceof FileNotFoundException) {
System.out.println(
"file could not be found, \n" +
"or could not be opened.\n" +
"Please investigate and try again.");
} // End if.
} // End try/ catch block.
} // End main.
}

// End class







public static void main(String[] args) {
JFrame frame = new convert3();

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

frame.pack();
frame.setVisible(true);
}
}



 
There is a system property named:

line.separator Line separator

("\n" on UNIX)
("\n\r" on Windows)

you could skip 1 or 2 bytes per line if your file is binary.

Or read the file using a BufferedReader it has a method readLine.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top