Share on Google+Share on Google+

sushant
IOException
1 Answer(s)      7 years ago
Posted in : Java Beginners

Ads

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
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
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 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
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
unicodetxtx file
main(String[] args)throws IOException{ int j=0; try{ BufferedReader i... : "); } catch(IOException e){ System.out.println(e.getMessage
Tutorials   
Java Spring Hibernate Struts Training What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code

Ads

 
Advertisement null

Ads