Saving Picture From Byte Arrays Using Random Access File
Jul 12, 2014
I'm trying to save a picture from byte arrays using RandomAccessFile. The file appears but doesn't open (like its corrupted).
I'm using the bittorent protocol which gives a SHA-1 hash that I compare all the bytes with to verify the data. All the bytes pass the hash check and all the hashes are checked. So I'm pretty sure I'm getting all the bytes correctly.
Is there anything I can do that could tell what's going wrong?
public RUBTClient(final TorrentInfo2 tInfo, final String outFileName) {
...
this.outFileName = outFileName;
File destined = new File(outFileName);
try {
destined_file = new RandomAccessFile(destined, "rw");
destined_file.setLength(tInfo.file_length);
} catch (FileNotFoundException e1) {
[Code] .....
View Replies
ADVERTISEMENT
May 3, 2014
I will write guess the picture game. I didn't find for example 4*4 picture how to sort picture random everytime ?
For example , when game start random various are 2734856127348561 two times same picture...
View Replies
View Related
Dec 8, 2014
I am working on a project that requires me to build a database with random access file, representing products, the base product contains a name (about 30 characters), a price (double), and a quantity (integer). I have worked on this project for probably 15+ hours and have tried so many things and feel like I've barley made any progress...
The part i am really struggling with is taking the data from the text file and creating an object array with it using the product class. Once ive accomplished that, i have to use that data to create a random access file with the data.
Here is the base Product class that must be used to create the objects for the array.
public class Product
{
public String pName;
public String stringName;
public double price;
public int quanity;
[Code] .....
And then here is the data from the text file that i must extract to use to create product objects.
Dill Seed,938,34
Mustard Seed,100,64
Coriander Powder,924,18
Turmeric,836,80
Cinnamon (Ground Korintje),951,10
Cinnamon (Ground) Xtra Hi Oil (2x),614,31
Cinnamon (Ground) High Oil (1X),682,19
These continue for about 40-50 entries, they are not separated by a blank line though i had to add those so it would display correctly, each entry is on its own line with name separated with spaces, then price after a comma, then quantity after the second comma.....
View Replies
View Related
Dec 9, 2014
I am working on a project that requires me to build a database with random access file, representing products, the base product contains a name (about 30 characters), a price (double), and a quantity (integer). I have worked on this project for probably 15+ hours and have tried so many things and feel like I've barley made any progress...
The part i am really struggling with is taking the data from the text file and creating an object array with it using the product class. Once ive accomplished that, i have to use that data to create a random access file with the data. Here is the base Product class that must be used to create the objects for the array.
public class Product
{
public String pName;
public String stringName;
public double price;
public int quanity;
[Code]...
these continue for about 40-50 entries, they are not seperated by a blank line though i had to add those so it would display correctly, each entry is on its own line with name seperated with spaces, then price after a comma, then quanity after the second comma.....
View Replies
View Related
Dec 8, 2014
I am working on a project that requires me to build a database with random access file, representing products, the base product contains a name (about 30 characters), a price (double), and a quantity (integer). I have worked on this project for probably 15+ hours and have tried so many things and feel like I've barley made any progress...
The part i am really struggling with is taking the data from the text file and creating an object array with it using the product class. Once ive accomplished that, i have to use that data to create a random access file with the data.
Here is the base Product class that must be used to create the objects for the array.
public class Product
{
public String pName;
public String stringName;
public double price;
public int quanity;
//Constructor
public Product( String pName, double price, int quanity )
[code]....
and then here is the data from the text file that i must extract to use to create product objects.
Dill Seed,938,34
Mustard Seed,100,64
Coriander Powder,924,18
Turmeric,836,80
Cinnamon (Ground Korintje),951,10
Cinnamon (Ground) Xtra Hi Oil (2x),614,31
Cinnamon (Ground) High Oil (1X),682,19
these continue for about 40-50 entries, they are not separated by a blank line though i had to add those so it would display correctly, each entry is on its own line with name separated with spaces, then price after a comma, then quanity after the second comma.....
View Replies
View Related
Oct 26, 2014
I need to write a program that will create an inventory file as a random access file. The problem that I am having is that among the things I need to add to the random access file is a "car name". Since random access files are byte based I don't know how I could possibly write and then read a string value.
View Replies
View Related
Jul 10, 2014
I have a random access file stream. I have below code I want to written data that is currently pointed by pointer till the end and display that data on System.out Stream but it is not working my code is
//Create a Random Access File
java.io.RandomAccessFile random=new java.io.RandomAccessFile("D:/input.txt","rw");
//Position the pointer to a seek
try {
random.seek(300);
long pointer=random.getFilePointer();
System.out.println("Current Pointer is:"+pointer);
int a;
while((a=random.read())!=-1) {
System.out.println(a);
}
Where am I wrong? Although it is returning actual position of pointer in pointer using
View Replies
View Related
May 4, 2015
I am creating a read/write, RandomAccessFile that I will use to store several student ID and gpa score. What I don't understand is how I should go about retrieving a particular set of data.
public class ReadWrite {
static Scanner keyboard = new Scanner(System.in);
static String fileName = "Stu.dat";
static RandomAccessFile raf;
public static void main(String [] args) throws FileNotFoundException, IOException {
[Code] .....
View Replies
View Related
Jul 11, 2014
I'm trying to write data for three products to a random access file, but I have a problem. Each product has a code and a price. After writing them to my file, I want to get to the second field or product. I do this by getting the length for each record, and multiplying it by the field number I want to go to. I use the result for the seek() method of the RandomAccessFile object. It is giving me an EOFException, but when I use 0 for the seek method i get the data for the first poduct or field.
Java Code:
public static void main(String[] args) {
final int RECORD_LENGTH = 16;
try(RandomAccessFile productsOutput = new RandomAccessFile("C:UsersalfonsoDesktopBinaryTestRandom.ran", "rw")) {
String[] codes = {"java", "jsps", "txtp"};
double[] prices = {49.5, 19.9, 29.9};
for(int i = 0; i < codes.length; i++) {
[Code] .....
View Replies
View Related
Sep 15, 2014
I need to send a byte array across a network. I know how to do this. (server->client)
byte[] myArray = new byte[]{0,1,2,3,4,5,6,7,8,9};
DataOutputStream.write(myArray);
... and I know how to receive it.
byte[] myArray = new byte[10];
DataInputStream.read(myArray);
When I send over one of these arrays, it ends up stopping storing values in the array when only about half the array is received, even though the array is sent from the server all at once. This results in the receiver's array, in this case, being something like {1,2,3,4,0,0,0,0,0,0}.
I can easily solve this - and already have - by simply adding a loop onto it and waiting for the bytes to all be received, as the method returns the amount of bytes actually read.
byte[] myArray = new byte[10];
int bytesRead = 0;
while( bytesRead < myArray.length) {
bytesRead += DataInputStream.read( myArray, bytesRead, myArray.length - bytesRead );
}
I am simply wondering if there is a better solution to this, as the current solution isn't that elegant. Did I do something wrong to cause only a part of the array to be sent first? Would it be better to use DataInputStream.readByte() to read off the bytes one by one rather than an array at once, and then store them in an array afterward? Would this cause a performance decrease as each byte is read individually? (I'm sending an array of several million bytes)
View Replies
View Related
Sep 21, 2014
So im making this ghost game where i display an 8x8 filled with 0s and a randomly generator five 1s in there I can get it to display 0s and add 1s, however sometimes the 1s that are randomly generated sometimes go on the same spot making it look like there are only four 1s. How would i go about fixing that?
package Grade12;
import java.util.Random;
public class Ghost {
public static void main(String[] args) {
Random generator = new Random();
int gameboard [][] = new int [8][8];
int randomx, randomy, counter = 0, sum = 0;
for(int row = 0; row < 8; row++){
for(int col = 0; col < 8; col++){
(gameboard[row][col]) = 0;
}
[code]...
View Replies
View Related
Dec 7, 2014
The problem you have is that it doesn't produce a 4x4 array. For that you need more nested for loops.This is what I have tried so far:
import java.util.Scanner;
import java.util.Random;
public class challenged2e
{
static Scanner console = new Scanner(System.in);
public static int i=0;
public static void main (String args[])
[code]...
I get 21 errors and they all have to do with i and j.
View Replies
View Related
Apr 8, 2014
We were suppose to make a program for an assignment and the prof provided some codes to start of. What does this basically mean? How do i access the string arrays from consolelist?
class ConsoleInfo {
private String conTitle;
private double conPrice;
private int conQty;
private String conPic;
private static String empPassword;
ConsoleInfo(String title, double price, int qty,String pic)
[Code]...
View Replies
View Related
Apr 30, 2014
i have I byte array ,That I was getting from the gps packets , I need to convert that into jpg file
public static void writejpegfile(byte[] someByteArray) throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream("image" + new Date().getTime() + ".jpg")
try {
[Code]....
View Replies
View Related
Oct 1, 2014
I am learning FileIO and I am having an ArrayIndexOutOfBoundsException for the following code and I am unsure why...
import java.io.*;
public class ArrayWriter {
public static void main(String[] args) {
PrintWriter fout = null;
int[] data = new int[10];
for(int i = 0; i<data.length; i++) {
data[i] = (int)(Math.random() * 101);
[Code] ....
View Replies
View Related
Oct 27, 2014
I was able to create the txt file. How to get it to output my code. I know it's an object and it is a way to save data.
package multiplicationFile;
import java.util.Scanner;
import java.io.*;
public class Multiplication {
public static void main(String args[]) throws IOException {
PrintWriter outF = new PrintWriter("multiplications.txt");
[Code] ....
View Replies
View Related
Feb 11, 2014
I and a friend are working with a project to create a file system, who manages a secondary memory simulated as a byte array in Java. We want the file system to be a hierarchical tree structure like in UNIX.
We have come quite far, but the paths are not handled correct. I seem to have mistaken the relative folder ./ for the root folder, but it should mean "working directory folder", ie, where I stand now. That is, if I stand in /dir1 as my "working directory" and make mkdir ./dir2 then should dir2 end up as subfolder in dir1. But with me it appears in the root.
View Replies
View Related
Jan 21, 2014
I am trying to save the data a person enters to a file. See code below. It is compiling however the file created only shows largest =89, i want the file to show all the data a person enters.
import java.util.*;
import java.io.*;
public class LargerOfThree3{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
[Code] ...
View Replies
View Related
Apr 16, 2015
I have a file that has the following text in it:
# Main configuration file for DEDServer
# Starting resource values
startingGold=1000000000
startingElixir=1000000000
[Code] ....
View Replies
View Related
Dec 7, 2014
Everything compiles but doesn't work like it should. What the idea is, is to be able to add class personnel objects and have them saved in a file, so that later on i would be able to see them.
Back to my problem: When i create a new "Personnel" and try to run Write and read, to see if my new "Personnel" is added to the list, it's not there at all... what do i do wrong?
import java.io.*;
class Personnel implements Serializable
//No other action required by Serializable interface.
{
private long payrollNum;
private String surname;
private String firstNames;
[Code] ....
View Replies
View Related
Jul 30, 2014
I get a Base64Encoded image string (GIF format) from an external system. While reading the byte array and saving the image to file, the image gets saved in landscape orientation. The code snippet is below.
String image = "R0lGODdhCAcgA=";
Base64Coder decoder = new Base64Coder();
byte [] decodeBytes = decoder.decode(image.getBytes());
ByteArrayInputStream stream = new ByteArrayInputStream(pngArray);
BufferedImage bImage = ImageIO.read(stream);
File label = new File("C:/labels/test.gif");
ImageIO.write(bImage, "gif", label);
I want to save the image in portrait orientation. Is there a way to do that?
View Replies
View Related
Dec 4, 2014
I have created this project and want to be able to add new members to my members arraylist, store the input in a .txt file and load the new members after closing and opening the program.
[URL] ....
View Replies
View Related
Mar 8, 2014
My code's not working and I don't know why. I'm trying to read numerical values from a file and saving all instances where a letter is entered instead of a number to a string to be referenced as an error at a later point on in the code. However, there's an error and like I've stated before, I don't know what caused it.
public static void validateData() throws IOException{
File myfile = new File("gradeInput.txt");
Scanner inputFile = new Scanner(myfile);
for (int i=0; i<33; i++){
if(inputFile.hasNextDouble()){
double d = inputFile.nextDouble();
if (d<0||d>99999){
[Code] ....
This is the error that returns.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at Paniagua_Grading.validateData(Paniagua_Grading.java:29)
at Paniagua_Grading.main(Paniagua_Grading.java:6)
View Replies
View Related
May 9, 2015
Im trying to make a tic tac toe game that you play against the computer using a random number generator and two dimensional arrays for the game board. Im not trying to make a GUI, the assignment is to have the board in the console, which I have done. I have run into a few problems with trying to get the computer player to correctly generate 2 integers and have those two integers be a place on the game board. Here is my code so far.
import java.util.Random;
import java.util.Scanner;
public class TicTacToe {
private static Scanner keyboard = new Scanner(System.in);
private static char[][] board = new char[3][3];
public static int row, col;
[Code] ....
View Replies
View Related
Jun 7, 2014
JFrame parentFrame = new JFrame();
File f12=new File("E:
ewfile.txt");
JFileChooser fileChooser = new JFileChooser();
fileChooser.setSelectedFile(f12);
fileChooser.setDialogTitle("Specify a file to save");
int userSelection = fileChooser.showSaveDialog(parentFrame);
[code]....
I want to save selected text file as html file in target directory....
View Replies
View Related
Apr 14, 2015
I am simulating a game. This involves creating two Player objects(either HumanPlayer or ComputerPlayer), a TheBoard object, a TheGame object and a TheGameManager object. In the TheGameManager, i am trying to get it so that i can save the two Player objects, the TheBoard object and the TheGame object in current use at some point in the game and then load them later at some point. However, after debugged, i have found that it doesnt like the writeObject() method and skips and sends an exception up.
My TheGameManager class has the following fields:
public class TheGameManager implements GameManager {
private Player player1;
private Player player2;
private TheBoard currentBoard;
private TheGame currentGame;
//private String[] args;
private int iterations = 1;
[Code] .....
View Replies
View Related