1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
/* Read file in byte array using FileInputStream This example shows how to read a file in byte array using Java FileInputStream class. This method should only be used when the file size is less than Integer.MAX_VALUE. */ import java.io.*; public class ReadFileByteArray { public static void main(String[] args) { //create file object File file = new File("C://FileIO//ReadString.txt"); try { //create FileInputStream object FileInputStream fin = new FileInputStream(file); /* * Create byte array large enough to hold the content of the file. * Use File.length to determine size of the file in bytes. */ byte fileContent[] = new byte[(int)file.length()]; /* * To read content of the file in byte array, use * int read(byte[] byteArray) method of java FileInputStream class. * */ fin.read(fileContent); //create string from byte array String strFileContent = new String(fileContent); System.out.println("File content : "); System.out.println(strFileContent); } catch(FileNotFoundException e) { System.out.println("File not found" + e); } catch(IOException ioe) { System.out.println("Exception while reading the file " + ioe); } } } /* Output would be File content : This file is for demonstration of how to read file in byte array using Java FileInputStream. */ |
nice example this works thx
these examples are really useful
Hi,
i am tryigb to figuer out the Math.random class, ti randomly creates numbers
of type Double, right? so if i want to create numbers between 0-100, your second method should work, isn’t it?
however, i copy paste your code the IDE, and it kept do give me the followign exceptions:
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Exception in thread “main”
Thanks, Gil
I am braziliam
Your example saved my life rsrsrsrs
[code]
//As you might know, Math.random() generates a random double where X<0
//To generate a random int using the same method (ie Math.random()) you do the //following:
public class RandomInt{
public static void main(String[] args){
int random = (int)(Math.random()*10)+5; //*see bottom
System.out.println(random);
}
}
//the “(int)” parses the double to an int value
//replace 10 with your range of numbers
//(ie if you want a number between 5 and 15 the range is 10 [15-5]
//replace the 5 with the staring number
//(ie if you want the lowest number in the range to be 5 then add 5)
//another eg.
//int ran = (int)(Math.random()*100)-50;
//will return a value in the range [-50;50]
[/code]
Can anyone tell me what this code does and what it means
[code]System.out.println((int)(6*Math.random())+1);[/code]
It means generate a random number from the six consecutive numbers starting from 1. That means any number from 1,2,3,4,5,6. It does NOT mean any number from 1 to 6.
How about this code?
[code]
System.out.println((int)(6*Math.random())+4);
[/code]
Will it give you random number from range 4-6? NO! It will give you random numbers from range 4-9! Reason: The six consecutive numbers starting from 4 are 4,5,6,7,8,9.
If you want random numbers from 4-6, you must use this code:
[code]
System.out.println((int)( (6-4+1) *Math.random())+4);
[/code]
OR
[code]
System.out.println((int)(3*Math.random())+4);
[/code]
for(int i=1;i<=5;i++){
System.out.println((int)(6*Math.random())+4);
output:
6
7
5
9
7
It will print out a random integer between 1 and 6 similar to the roll of a die.
It is good
And what if you want to generate number from 0 to 255 (like R,G,B for making color).. How looks code for that?
//I have make a simple program for Game by using your code
import java.util.*;
class RandomGame
{
public static void main (String args[])
{
Scanner scan=new Scanner(System.in);
int a=0;
for(int j=0;j<5;j++)
{
//System.out.println((int)(Math.random()*100));
a=scan.nextInt();
if(a==((int)(Math.random()*100)))
System.out.println(“Congrats”);
else
System.out.println(“Try Again”);
}
System.out.println(“Game Over”);
}
}
//Waliullah
//Electrical Engineering Department
I was not initializing the byte array
* byte fileContent[] = new byte[(int)file.length()]; *
Now my prog is working.
Thanks!
If the file length is more than Integer maximum value , the program will not work
(int)(Math.random()*100)
Can you modify this code For Sequence Number Generation…..!
good one..
Don’t forget to close the FileInputStream!
Awesome!!! Worked like a charm!! 🙂