answersLogoWhite

0

What is DataInputStream?

Updated: 8/9/2023
User Avatar

Wiki User

11y ago

Best Answer

This section demonstrates you the use of DataInputStream class.

The class DataInputStream allows to read primitive Java data types from an underlying input stream. It reads the file line by line.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is DataInputStream?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Electricity bill preparation source codes in java?

import java.io.*; import java.lang.*; class electricity extends Exception { int b1; electricity(int b) { b1=b; System.out.println("\n\t\t Exception will caught below "); } } class electric { static void compute(int a,int b,String name,String bno)throws IOException,electricity { name=name; bno=bno; if(b<a) throw new electricity(b); else { int tmr,amt=0,z=0; tmr=b-a; do { DataInputStream in=new DataInputStream(System.in); System.out.println("\n\t Click any purpose "); System.out.println("\n\t\t 1-Domestic "); System.out.println("\n\t\t 2-Agriculture "); System.out.println("\n\t\t 3-Industry "); System.out.println("\n\t\t 4.Exit"); System.out.println("\n\n\t\t Enter ur Choice :"); int ch=Integer.parseInt(in.readLine()); switch(ch) { case 1: if(tmr<=100) amt =tmr *2; else if(tmr >100 && tmr <=200) amt=(tmr-100) *4 +100*2; System.out.println("\n\n\t\t **********************************************"); System.out.println("\t\t\t Electricity Bill Preparation "); System.out.println("\t\t **********************************************\n\n"); System.out.println("\n\t\t Name : " +name + " Bill no : " + bno ); System.out.println("\n\t\t Previous Meter Reading : " +a ); System.out.println("\n\t\t Closed Meter Reading :" +b ); System.out.println("\n\t\t Current Meter Reading :" +tmr ); System.out.println("\n\t\t Purpose : Domestic " ); System.out.println("\n\t\t Total Amount Rs. :" +amt ); break; case 2: if(tmr<=100) amt =tmr *0; else if(tmr >100 && tmr<=200) amt=(tmr-100) *1 +100*0; System.out.println("\n\n\t\t **********************************************"); System.out.println("\t\t\t Electricity Bill Preparation "); System.out.println("\t\t **********************************************\n\n"); System.out.println("\n\t\t Name : " +name + " Bill no : " + bno ); System.out.println("\n\t\t Previous Meter Reading : " +a ); System.out.println("\n\t\t Closed Meter Reading :" +b ); System.out.println("\n\t\t Current Meter Reading :" +tmr ); System.out.println("\n\t\t Purpose : Agriculture " ); System.out.println("\n\t\t Total Amount Rs. :" +amt ); break; case 3: if(tmr<=100) amt =tmr *4; else if(tmr >100 && tmr<=200) amt=(tmr-100) *8 +100*4; System.out.println("\n\n\t\t **********************************************"); System.out.println("\t\t\t Electricity Bill Preparation "); System.out.println("\t\t **********************************************\n\n"); System.out.println("\n\t\t Name : " +name + " Bill no : " + bno ); System.out.println("\n\t\t Previous Meter Reading : " +a ); System.out.println("\n\t\t Closed Meter Reading :" +b ); System.out.println("\n\t\t Current Meter Reading :" +tmr ); System.out.println("\n\t\t Purpose : Industry " ); System.out.println("\n\t\t Total Amount Rs. :" +amt ); break; case 4: System.exit(0); } System.out.println("\n\t Continue :"); z=Integer.parseInt(in.readLine()); } while(z==1); } } public static void main(String ar[])throws IOException { electric ee=new electric(); String name,bno; int pmr,cmr; DataInputStream in=new DataInputStream(System.in); System.out.println("\n\t ELECTRICITY BILL USING USERDEFINED EXCEPTION "); System.out.print("\n\t\t Enter the name : "); name=in.readLine(); System.out.print("\n\t\t Enter the Bill Number :"); bno=in.readLine(); System.out.print("\n\t\t Previous Meter Reading : "); pmr=Integer.parseInt(in.readLine()); System.out.print("\n\t\t Current Meter Reading : "); cmr=Integer.parseInt(in.readLine()); try { compute(pmr,cmr,name,bno); } catch(Exception e) { System.out.println("\n\t\tUserDefined Exception is found"); } } }


File Upload into Database in JSP?

<%@page language="java" session="true" import="java.io.*,java.util.*,java.io.*,java.sql.*,javax.servlet.*"%> <% //to get the content type information from JSP Request Header String contentType = request.getContentType(); if (contentType != null && contentType.indexOf("multipart/form-data") >= 0) { DataInputStream in = new DataInputStream(request.getInputStream()); //we are taking the length of Content type data int formDataLength = request.getContentLength(); byte dataBytes[] = new byte[formDataLength]; int byteRead = 0; int totalBytesRead = 0; //this loop converting the uploaded file into byte code while (totalBytesRead < formDataLength) { byteRead = in.read(dataBytes, totalBytesRead, formDataLength); totalBytesRead += byteRead; } String file = new String(dataBytes); //for saving the file name String saveFile = file.substring(file.indexOf("filename="") + 10); saveFile = saveFile.substring(0, saveFile.indexOf("\n")); saveFile = saveFile.substring(saveFile.lastIndexOf("") + 1,saveFile.indexOf(""")); int lastIndex = contentType.lastIndexOf("="); String boundary = contentType.substring(lastIndex + 1); int pos; //extracting the index of file pos = file.indexOf("filename=""); pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; int boundaryLocation = file.indexOf(boundary, pos) - 4; int startPos = file.substring(0, pos).getBytes().length; int endPos = file.substring(0, boundaryLocation).getBytes().length; // creating a new file with the same name and writing the content in new file //FileOutputStream fileOut = new FileOutputStream(saveFile); FileOutputStream fileOut = new FileOutputStream("C:\\Program Files\\Apache Software Foundation\\Tomcat 5.0\\webapps\\incidentreportform_main\\upload"+saveFile+""); fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.flush(); fileOut.close(); //out.println(saveFile); Connection con=null,con1=null; Statement stmt=null,stmt1=null; PreparedStatement ps=null,ps1=null; ResultSet rs=null,rs1=null; String sql="",sql1="",a=""; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:inficert","inficert","inficert"); stmt=con.createStatement(); String sno=request.getParameter("parameter"); int j1=0; try { sql="UPDATE department set crftitle =('"+saveFile+"') where sno='"+sno+"'"; j1=stmt.executeUpdate(sql); if(j1!=0) { %> <script type="text/javascript"> alert('Successfully Uploaded'); top.location.href = "selectlocation.jsp"; </script> <% } } catch(Exception ex) { ex.printStackTrace(); } } %>


What is the use of BufferedReader in Java programming?

The BufferedReader class provides buffering to your Reader's. Buffering can speed up IO quite a bit. Rather than read one character at a time from the network or disk, you read a larger block at a time. This is typically much faster, especially for disk access and larger data amounts. The main difference between BufferedReader and BufferedInputStream is that Reader's work on characters (text), wheres InputStream's works on raw bytes. To add buffering to your Reader's simply wrap them in a BufferedReader. Here is how that looks: Reader input = new BufferedReader( new FileReader("c:\\data\\input-file.txt"));


Related questions

What is the difference between datainputstream and bufferedreader?

DataInputStream is used for reading primitive data types from an input stream, while BufferedReader is used for reading characters from a character-input stream and buffering the characters for efficient reading. DataInputStream is useful when reading binary data, while BufferedReader is more suitable for reading text data in a more optimized way.


What difference between InputStreamReader and DatainputStream in java?

Both are same


Java program-read a string and find out the frequency of each letter appeared in the string?

import java.io.*; public class Frequencyofchar { public static void main(String a[]) { try { DataInputStream d=new DataInputStream(System.in); System.out.println("Enter a String"); String s=d.readLine(); s.toLowerCase(); StringBuilder sb=new StringBuilder(s); char ch=0; int i,j,cn; for(i=0;i<s.length();i++) { cn=0; for(j=i;j<s.length();j++) { if(s.charAt(i)==s.charAt(j)) { cn++; ch=s.charAt(i); if(i<j) { sb.deleteCharAt(j); s=sb.toString(); j--; } } } System.out.println(ch+" occurs "+cn+" times"); } } catch(Exception e) { } } }


What is bufferedReader in advance java?

BufferedReader Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. The buffer size may be specified, or the default size may be used. The default is large enough for most purposes. In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example, BufferedReader in = new BufferedReader(new FileReader("foo.in")); will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient. Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader.


Electricity bill preparation source codes in java?

import java.io.*; import java.lang.*; class electricity extends Exception { int b1; electricity(int b) { b1=b; System.out.println("\n\t\t Exception will caught below "); } } class electric { static void compute(int a,int b,String name,String bno)throws IOException,electricity { name=name; bno=bno; if(b<a) throw new electricity(b); else { int tmr,amt=0,z=0; tmr=b-a; do { DataInputStream in=new DataInputStream(System.in); System.out.println("\n\t Click any purpose "); System.out.println("\n\t\t 1-Domestic "); System.out.println("\n\t\t 2-Agriculture "); System.out.println("\n\t\t 3-Industry "); System.out.println("\n\t\t 4.Exit"); System.out.println("\n\n\t\t Enter ur Choice :"); int ch=Integer.parseInt(in.readLine()); switch(ch) { case 1: if(tmr<=100) amt =tmr *2; else if(tmr >100 && tmr <=200) amt=(tmr-100) *4 +100*2; System.out.println("\n\n\t\t **********************************************"); System.out.println("\t\t\t Electricity Bill Preparation "); System.out.println("\t\t **********************************************\n\n"); System.out.println("\n\t\t Name : " +name + " Bill no : " + bno ); System.out.println("\n\t\t Previous Meter Reading : " +a ); System.out.println("\n\t\t Closed Meter Reading :" +b ); System.out.println("\n\t\t Current Meter Reading :" +tmr ); System.out.println("\n\t\t Purpose : Domestic " ); System.out.println("\n\t\t Total Amount Rs. :" +amt ); break; case 2: if(tmr<=100) amt =tmr *0; else if(tmr >100 && tmr<=200) amt=(tmr-100) *1 +100*0; System.out.println("\n\n\t\t **********************************************"); System.out.println("\t\t\t Electricity Bill Preparation "); System.out.println("\t\t **********************************************\n\n"); System.out.println("\n\t\t Name : " +name + " Bill no : " + bno ); System.out.println("\n\t\t Previous Meter Reading : " +a ); System.out.println("\n\t\t Closed Meter Reading :" +b ); System.out.println("\n\t\t Current Meter Reading :" +tmr ); System.out.println("\n\t\t Purpose : Agriculture " ); System.out.println("\n\t\t Total Amount Rs. :" +amt ); break; case 3: if(tmr<=100) amt =tmr *4; else if(tmr >100 && tmr<=200) amt=(tmr-100) *8 +100*4; System.out.println("\n\n\t\t **********************************************"); System.out.println("\t\t\t Electricity Bill Preparation "); System.out.println("\t\t **********************************************\n\n"); System.out.println("\n\t\t Name : " +name + " Bill no : " + bno ); System.out.println("\n\t\t Previous Meter Reading : " +a ); System.out.println("\n\t\t Closed Meter Reading :" +b ); System.out.println("\n\t\t Current Meter Reading :" +tmr ); System.out.println("\n\t\t Purpose : Industry " ); System.out.println("\n\t\t Total Amount Rs. :" +amt ); break; case 4: System.exit(0); } System.out.println("\n\t Continue :"); z=Integer.parseInt(in.readLine()); } while(z==1); } } public static void main(String ar[])throws IOException { electric ee=new electric(); String name,bno; int pmr,cmr; DataInputStream in=new DataInputStream(System.in); System.out.println("\n\t ELECTRICITY BILL USING USERDEFINED EXCEPTION "); System.out.print("\n\t\t Enter the name : "); name=in.readLine(); System.out.print("\n\t\t Enter the Bill Number :"); bno=in.readLine(); System.out.print("\n\t\t Previous Meter Reading : "); pmr=Integer.parseInt(in.readLine()); System.out.print("\n\t\t Current Meter Reading : "); cmr=Integer.parseInt(in.readLine()); try { compute(pmr,cmr,name,bno); } catch(Exception e) { System.out.println("\n\t\tUserDefined Exception is found"); } } }


File Upload into Database in JSP?

<%@page language="java" session="true" import="java.io.*,java.util.*,java.io.*,java.sql.*,javax.servlet.*"%> <% //to get the content type information from JSP Request Header String contentType = request.getContentType(); if (contentType != null && contentType.indexOf("multipart/form-data") >= 0) { DataInputStream in = new DataInputStream(request.getInputStream()); //we are taking the length of Content type data int formDataLength = request.getContentLength(); byte dataBytes[] = new byte[formDataLength]; int byteRead = 0; int totalBytesRead = 0; //this loop converting the uploaded file into byte code while (totalBytesRead < formDataLength) { byteRead = in.read(dataBytes, totalBytesRead, formDataLength); totalBytesRead += byteRead; } String file = new String(dataBytes); //for saving the file name String saveFile = file.substring(file.indexOf("filename="") + 10); saveFile = saveFile.substring(0, saveFile.indexOf("\n")); saveFile = saveFile.substring(saveFile.lastIndexOf("") + 1,saveFile.indexOf(""")); int lastIndex = contentType.lastIndexOf("="); String boundary = contentType.substring(lastIndex + 1); int pos; //extracting the index of file pos = file.indexOf("filename=""); pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; int boundaryLocation = file.indexOf(boundary, pos) - 4; int startPos = file.substring(0, pos).getBytes().length; int endPos = file.substring(0, boundaryLocation).getBytes().length; // creating a new file with the same name and writing the content in new file //FileOutputStream fileOut = new FileOutputStream(saveFile); FileOutputStream fileOut = new FileOutputStream("C:\\Program Files\\Apache Software Foundation\\Tomcat 5.0\\webapps\\incidentreportform_main\\upload"+saveFile+""); fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.flush(); fileOut.close(); //out.println(saveFile); Connection con=null,con1=null; Statement stmt=null,stmt1=null; PreparedStatement ps=null,ps1=null; ResultSet rs=null,rs1=null; String sql="",sql1="",a=""; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:inficert","inficert","inficert"); stmt=con.createStatement(); String sno=request.getParameter("parameter"); int j1=0; try { sql="UPDATE department set crftitle =('"+saveFile+"') where sno='"+sno+"'"; j1=stmt.executeUpdate(sql); if(j1!=0) { %> <script type="text/javascript"> alert('Successfully Uploaded'); top.location.href = "selectlocation.jsp"; </script> <% } } catch(Exception ex) { ex.printStackTrace(); } } %>


What is the use of BufferedReader in Java programming?

The BufferedReader class provides buffering to your Reader's. Buffering can speed up IO quite a bit. Rather than read one character at a time from the network or disk, you read a larger block at a time. This is typically much faster, especially for disk access and larger data amounts. The main difference between BufferedReader and BufferedInputStream is that Reader's work on characters (text), wheres InputStream's works on raw bytes. To add buffering to your Reader's simply wrap them in a BufferedReader. Here is how that looks: Reader input = new BufferedReader( new FileReader("c:\\data\\input-file.txt"));


Explain different stream classes in Java?

Byte StreamsIf a program needs to read/write bytes (8-bit data), it could use one of the subclasses of the InputStream or OutputStream respectively. The example below shows how to use the class FileInputStream to read a file named abc.dat. This code snippet prints each byte's value separated with white spaces. Byte values are represented by integers from 0 to 255, and if the read() method returns -1, this indicates the end of the stream.import java.io.FileInputStream;import java.io.IOException;public class ReadingBytes {public static void main(String[] args) { FileInputStream myFile = null; try {myFile = new FileInputStream("c:\\abc.dat"); // open the streamboolean eof = false;while (!eof) {int byteValue = myFile.read(); // read the streamSystem.out.println(byteValue);if (byteValue == -1){eof = true;}//myFile.close(); // do not do it here!!!}}catch (IOException e) {System.out.println("Could not read file: " + e.toString());} finally{try{if (myFile!=null){myFile.close(); // close the stream}} catch (Exception e1){e1.printStackTrace();}}}}Please note that the stream is closed in the clause finally. Do not call the method close() inside of the try/catch block right after the file reading is done. In case of exception during the file read, the program would jump over the close() statement and the stream would never be closed!The next code fragment writes into the file xyz.dat using the class FileOutputStream:int somedata[]={56,230,123,43,11,37};FileOutputStream myFile = null;try {myFile = new FileOutputStream("c:\xyz.dat");for (int i = 0; i BufferedInputStream --> DataInputStreamFileInputStream myFile = new FileInputStream("myData.dat");BufferedInputStream buff = new BufferedInputStream(myFile);DataInputStream data = new DataInputStream(buff);try {int num1 = data.readInt();int num2 = data.readInt();float num2 = data.readFloat();float num3 = data.readFloat();float num4 = data.readFloat();double num5 = data.readDouble();} catch (EOFException eof) {...}