Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


sushant
IOException
1 Answer(s)      5 years and a month ago
Posted in : Java Beginners



Advertisement
View Answers

July 28, 2009 at 5:08 PM


Hi Friend,

Actually you are reading a file that has been created but it does not contain any data.Therefore error occurs.So use writeBytes() method first to write the data into the file and then read that file data using readByte() method.

Here is the modified code:

import java.io.File;
import java.io.RandomAccessFile;
import java.io.IOException;
public class randomdemo {
private static void doAccess() {
try {
File file = new File("DemoRandomAccessFile.out");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.seek(file.length());
raf.write(0x0A);
raf.writeBytes("This will complete the Demo");
int i=(int)raf.length();
System.out.println("Length: " + i);
raf.seek(0);
for(int ct = 0; ct < i; ct++){
byte b = raf.readByte();
System.out.print((char)b);
}
raf.close();
} catch (IOException e) {
System.out.println("IOException:");
e.printStackTrace();
}
}
public static void main(String[] args) {
doAccess();
}
}

Thanks


Related Tutorials/Questions & Answers:
throws IOException - Java Beginners
throws IOException  throws IOException means   Hi Friend, Most methods of IO class throw an IOException if anything goes wrong. Therefore this block of code is used with the methods that may be having some
IOException - Java Beginners
"); raf.close(); } catch (IOException e) { System.out.println("IOException:"); e.printStackTrace... (IOException e) { System.out.println("IOException:"); e.printStackTrace
Advertisements
java.sql.SQLException: Network error IOException: No buffer space available (maximum connections reached?): connect
java.sql.SQLException: Network error IOException: No buffer space available (maximum connections reached?): connect  Once in a while i am facing.... java.sql.SQLException: Network error IOException: No buffer space available (maximum
Java ioexception
Java ioexception       Exceptions in java is the way of indicating the occurrence of abnormal condition... representing the problem occurred. The IOException is thrown to indicate some sort
URLInformation
URLInformation       Here we are going to explain the method to find out the URL information. This program defines the IOException for the exception handling
EchoClientSocket
EchoClientSocket       In this section, we are going to explain the method to find out the hostname port number of a local machine. This program defines the IOException
Coding - Java Beginners
Coding  Code a try statement that catches an IOException and an EOFException. If an IOException occurs, print the message ?An I/O error occurred... EOFException extends IOException{ private IOException _ex; public
exceptions
exceptions  why we get compiletime exceptions(checkedExeption)? forEg:IOException,servletException
Scanner
Scanner  In java,while connectig keyboard through java.util.scanner class..it does not throws IOException..why
java help - Java Beginners
java help  Code a try statement that catches an IOException and an EOFException. If an IOException occurs, print the message ?An I/O error occurred.? to the console. If an EOFException occurs, print the message ?End of file
I want to Transfer only 1/3rd of contents from one folder to other but my code is transferring all contents
(File src, File dest) throws IOException { if (src.isDirectory...(); if (entries == null) { throw new IOException... { dest.createNewFile(); } catch (IOException ex
java help - Java Beginners
an IOException.  Hi friend, Code a statement to test a catch block that catches an IOException. import java.io.*; class IOExample { public static... subtotal = Double.parseDouble(bufReader.readLine()); } catch (IOException e
Java I/O Object Streams
of bytes.int available() throws IOException close() : This method is used to close the input stream.void close() throws IOException read() : This method is used to read the data's byte.int read() throws IOException read(byte[] b
SCJP Module-6 Question-28
() throws IOException{ super.process(); System.out.print("Inside B "); throw new IOException(); } public static void main(String[] args){ try { new B().process(); } catch (IOException e) { System.out.println("Exception
java help - Java Beginners
java help  Code a catch block that catches an IOException, prints... that catches an IOException : import java.io.*; public class CreateFile1..."); } } catch (IOException ioe) { System.out.println("An I/O exception occurred
Java I/O Buffered Streams
stream. public int available() throws IOException close... to the stream get released. public void close() throws IOException  ... read() throws IOException   read(byte[] b, int off
Exception - Java Beginners
Exception  plz explain to me with an example the use of try catch,throw and throws in java. void accept() throws IOException can a catch statement follow this method to handle IOException?if not where is it handled?  
Java I/O Data Streams
() throws IOException   readByte() : This method...() throws IOException   readChar() : This method is used to read two input...() throws IOException   readDouble() : This method is used to read
SCJP Module-6 Question-14
SuperClass { public void MyMethod() throws IOException { super.MyMethod(); System.out.print("SubClass,"); throw new IOException(); } public static... (IOException e) { System.out.println("Exception"); } } } What
provide source - Java Beginners
."); mue.printStackTrace(); System.exit(1); } catch (IOException ioe) { System.out.println("Oops- an IOException happened."); ioe.printStackTrace... (IOException ioe) {} } } } Thanks
Servlets
(HttpServletRequest req,HttpServletResponse res) throws Se rvletException,IOException...,IOException { ^ InsertServlet.java:8... Se rvletException,IOException
JAVA
,HttpServletResponse res) throws Se rvletException,IOException...,HttpServletResponse res) throws Se rvletException,IOException...(HttpServletRequest req,HttpServletResponse res) throws Se rvletException,IOException
Sort
class IOException { public IOException() { } } ArithmeticException e ); { System.out.println("Cannot calculate average - no data"); catch(IOException
Java Io BufferedWriter
with the stream. Syntax : public void close() throws IOException   flush...() throws IOException   newLine() : This method is used for separating a line... : public void newLine() throws IOException   write(char[] cbuf, int off, int
SCJP Module-11 Question-6
through IOException after class declaration. Answer: (B) & (C
Hello World in servlet
, IOException { PrintWriter out = res.getWriter(); out.println("Hello
java - Java Beginners
ReadContent { public static byte[] BytesFromFile(File file) throws IOException...) { throw new IOException("Could not completely read file... IOException{ /* BufferedReader buff = new BufferedReader(new InputStreamReader
Java FilterOutputStream Example
. Syntax : public void close() throws IOException   flush() : This method... bytes to the stream. Syntax : public void flush() throws IOException  ... to the stream. Syntax : public void write(byte[] b) throws IOException   write
java help - Java Beginners
not found.? at the console. It should not catch an IOException. Use...[])throws IOException { FileInputStream fis=null; try{ fis = new FileInputStream
Finalize method in Java - Java Beginners
FinalizeFileOutputStream() throws IOException{ super("Rose India"); // call the super class constructor. } public static void main(String[] args) throws IOException
unicodetxtx file
main(String[] args)throws IOException{ int j=0; try{ BufferedReader i... : "); } catch(IOException e){ System.out.println(e.getMessage
Java IO FilterReader
() throws IOException   mark(int readAheadLimit) : This method is used to mark... readAheadLimit) throws IOException   markSupported() : This method returns a boolean... is used to read a character. Syntax : public int read() throws IOException
Java IO StringReader
associated with the stream. Syntax : public void close() throws IOException  ... IOException   markSupported() : This method returns a boolean value which... a character. Syntax : public int read() throws IOException   read(char
I have one small doubt , does this code read Microsoft Office suit file format data such as .doc , .docx etc ?
); } } catch (IOException e) { e.printStackTrace...) objReader.close(); } catch (IOException ex) { ex.printStackTrace
Java FileInputStream
() throws IOException close() This method is used to close the input stream... : public void close() throws IOException getFD() This method is used...() throws IOException read() This method is used to read byte of data from
Java IO Reader
() throws IOException   read(char[] cbuf, int off, int len... : public abstract int read(char[] cbuf, int off, int len) throws IOException  ... : public void mark(int readAheadLimit) throws IOException  
Java IO PipedWriter
(PipedReader snk) throws IOException Method Detail close() : This method... with the stream. Syntax : public void close() throws IOException  ... IOException   flush() : This method is used to flush the stream
Java ObjectOutputStream
private data. Syntax : protected ObjectOutputStream() throws IOException... ObjectOutputStream(OutputStream out) throws IOException Methods... : public final void writeObject(Object obj) throws IOException   close
boolean help
boolean access=false; public static String enterName() throws IOException... IOException{ String[] names={"Michael","Gavin","Morgan","Edward","Philippa...; } } } public static void main(String[] args) throws IOException
Java IO OutputStream
void close() throws IOException   flush() : This method is used... IOException   write(byte[] b) : This method is used to write the b.length...[] b) throws IOException   write(byte[] b, int off, int len
illegal start of type
("FileNotFoundException : " + ex); }catch(IOException ioe) { System.out.println("IOException : " + ioe); } }   hi friend, Check your code properly...()); fos.close(); } catch (IOException e1) { // TODO
Java I/O Character Streams
() throws IOException   mark(int readAheadLimit) : This method... readAheadLimit) throws IOException   markSupported() : This method... of the stream. public int read() throws IOException   read(char
SCJP Module-2 Question-4
; catch(IOException e){}} 8.  void check() throws IOException{ 9. ... IOException();}} 11. class Subexample4 extends Example4 { 12. void check() { 13
SCJP Module-6 Question-15
extends SuperClass { 6 public void MyMethod() throws IOException { 7 super.MyMethod(); 8 System.out.print("SubClass,"); 9 throw new IOException...().MyMethod(); 14} catch (IOException e) { 15 System.out.println("
SCJP Module-6 Question-29
B extends A1{ 4   public void process() throws IOException{ 5 ...;); 7   throw new IOException(); 8   } 9   public... (IOException e) { System.out.println("Exception is caught "); }}} How can we
Reverse - Java Beginners
City{ public static void main(String[]args)throws IOException... IOException{ int count = 0; String inputLine = inputLine(br...(BufferedReader br)throws IOException{ String inputLine = br.readLine
i need help - Development process
catch (IOException e) { System.out.println(e); } at present we have to give..."); } in.close(); bw.close(); } catch (IOException ioe) { System.err.println("IOEXception occured : " + ioe.getMessage
SERVLETS
(HttpServletRequest req,HttpServletResponse res) throws Se rvletException,IOException...(HttpServletRequest req,HttpServletResponse res) throws Se rvletException,IOException... rvletException,IOException
Sorting and Searching
) { catch( private static class IOException { public IOException... average - no data"); catch(IOException
How send different files to browser at same time ..example(pdf&html) want display in same request using servlet
, IOException { performTask(request, response); } protected void doPost..., IOException { performTask(request, response); } private void... ServletException, IOException { String pdfFileName = "Java Tips.pdf
 

 

 

DMCA.com