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



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


sushant
IOException
1 Answer(s)      5 years and 4 months 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
Scanner
Scanner  In java,while connectig keyboard through java.util.scanner class..it does not throws IOException..why
exceptions
exceptions  why we get compiletime exceptions(checkedExeption)? forEg:IOException,servletException
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
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
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
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
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 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
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
Sort
class IOException { public IOException() { } } ArithmeticException e ); { System.out.println("Cannot calculate average - no data"); catch(IOException
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
provide source - Java Beginners
."); mue.printStackTrace(); System.exit(1); } catch (IOException ioe) { System.out.println("Oops- an IOException happened."); ioe.printStackTrace... (IOException ioe) {} } } } Thanks
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
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
Hello World in servlet
, IOException { PrintWriter out = res.getWriter(); out.println("Hello
SCJP Module-11 Question-6
through IOException after class declaration. Answer: (B) & (C
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
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 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
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
 

 

 

DMCA.com