IOException

IOException

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
number 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
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
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
;. boolean readBoolean() throws IOException   readByte...() throws IOException   readChar() : This method is used... readChar() throws IOException   readDouble() : This method
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
the resources associated with the stream. Syntax : public void close() throws IOException... void flush() throws IOException   newLine() : This method is used.... Syntax : public void newLine() throws IOException   write(char[] cbuf
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
with the stream. Syntax : public void close() throws IOException   flush... IOException   write(byte[] b) : This method is used to write out the bytes... IOException   write(byte[] b, int off, int len) : This method is used to 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
Java IO FilterReader
void close() throws IOException   mark(int readAheadLimit) : This method...(int readAheadLimit) throws IOException   markSupported() : This method...() throws IOException   read(char[] cbuf, int off, int len) : This method
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 I/O Buffered Streams
. public int available() throws IOException close... to the stream get released. public void close() throws IOException  ... read() throws IOException   read(byte[] b, int off
Java FileInputStream
int available() throws IOException close() This method is used to close... with the stream. Syntax : public void close() throws IOException getFD... FileDescriptor getFD() throws IOException read() This method is used to read
Java IO Reader
abstract void close() throws IOException   read(char[] cbuf, int off, int... IOException   Commonly used methods of this class are as follows :ADS... IOException   markSupported() : This method returns a boolean value
Java IO PipedWriter
: public PipedWriter(PipedReader snk) throws IOException Method Detail close... IOException   connect(PipedReader snk) : This method is used to connect the piped...(PipedReader snk) throws IOException   flush() : This method is used to flush
Java ObjectOutputStream
IOException, SecurityException ObjectOutputStream(OutputStream out.... Syntax :public ObjectOutputStream(OutputStream out) throws IOException... stream. Syntax : public final void writeObject(Object obj) throws IOException
boolean help
IOException{ System.out.println("Enter your first name"); BufferedReader... checkName() throws IOException{ String[] names={"Michael","Gavin","Morgan","Edward... IOException { checkName(); if(access==true){ System.out.println("Access
Java IO OutputStream
released. public void close() throws IOException   flush...() throws IOException   write(byte[] b) : This method is used to write... void write(byte[] b) throws IOException   write(byte[] b, int
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... mark(int readAheadLimit) throws IOException   markSupported... if it reaches the end of the stream. public int read() throws IOException
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
Java I/O Byte Streams
the input stream. public int available() throws IOException   close... to the stream is get released. public void close() throws IOException  ...() throws IOException   read(byte[] b) : This method
groupby functionality in java
); } }catch(FileNotFoundException fnot){} catch(IOException io... (IOException ex) { ex.printStackTrace...; } } } catch(IOException io

Ads