JAVA Filing related

S

sbq

I am writing some 32 bit integer data in matlab using statements such
as:

f=fopen('temp','w');
a=int32(23);
fprintf(f,'%d',a);
fclose(f);

and trying to read in in JAVA using:
public static void main(String[] args) {
// TODO code application logic here

File file = new File("C:\\JAVA\\temp");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;

try {
fis = new FileInputStream(file);

// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
int a;
a = dis.readInt();
System.out.print(a);
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();


} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}//end catch2
}//end main


======================

Now logically, reading a 32 bit integer using this command should work
fine, but I am getting out of range values, like, if i am just reading
in integer valued 32, it would rather print 8924558 or something like
that, garbage values. I have tried using read other reading methods as
well such as readByte, readShort, read etc but only thing that works
is readln, which I don't want. It brings in whole line of data, which
i don't want. Rather, I need individual integers to be read, perhaps
delimited by tabs or new lines
 
J

Joshua Cranmer

sbq said:
Now logically, reading a 32 bit integer using this command should work
fine, but I am getting out of range values, like, if i am just reading
in integer valued 32, it would rather print 8924558 or something like
that, garbage values. I have tried using read other reading methods as
well such as readByte, readShort, read etc but only thing that works
is readln, which I don't want. It brings in whole line of data, which
i don't want. Rather, I need individual integers to be read, perhaps
delimited by tabs or new lines

A DataInputStream reads data as if it were binary, so the value `32'
would be written as `0011001100110010' in the file and then that
translated as an integer.

The easiest way to do what you want to do is to use Scanner:
<http://java.sun.com/javase/6/docs/api/java/util/Scanner.html>.
 
P

Patricia Shanahan

sbq said:
I am writing some 32 bit integer data in matlab using statements such
as:

f=fopen('temp','w');
a=int32(23);
fprintf(f,'%d',a);
fclose(f);
....

Did you mean to use text or binary? To do it in binary, compatible with
using DataInputStream, the Matlab code should be something like:

f = fopen('temp','w','ieee-be');
fwrite({f,a,'int32');

Java DataInputStream is big-endian and uses IEEE 754 format for floats,
so 'ieee-be' is the corresponding binary format for Matlab. "a" can be
any integer expression - I actually use the result of size applied to a
matrix, a pair of integers.

I've extracted this from some Java code that generates scripts and
invokes Matlab, so there may be detail errors, but the concept
definitely works.

I have a small package for using Matlab from Java. I can send you a copy
if that happens to be where you are heading.

Patricia
 
S

sbq

...

Did you mean to use text or binary? To do it in binary, compatible with
using DataInputStream, the Matlab code should be something like:

f = fopen('temp','w','ieee-be');
fwrite({f,a,'int32');

Java DataInputStream is big-endian and uses IEEE 754 format for floats,
so 'ieee-be' is the corresponding binary format for Matlab. "a" can be
any integer expression - I actually use the result of size applied to a
matrix, a pair of integers.

I've extracted this from some Java code that generates scripts and
invokes Matlab, so there may be detail errors, but the concept
definitely works.

I have a small package for using Matlab from Java. I can send you a copy
if that happens to be where you are heading.

Patricia

thanks Patricia. The package may certainly be useful. Most of my work
is in MATLAB/VC but had to switch over to JAVA for Interfacing few
modules.
 
P

Patricia Shanahan

sbq said:
thanks Patricia. The package may certainly be useful. Most of my work
is in MATLAB/VC but had to switch over to JAVA for Interfacing few
modules.

My situation is the other way round. I have a Java program, in which I
needed to do a truncated singular value decomposition on a moderately
large sparse matrix, and Matlab svds was exactly what I needed.

Assuming your posting e-mail address is valid, expect an e-mail from me
sometime in the next few days with an attached zip file.

Patricia
 
A

Andrew Thompson

sbq wrote:
...
...The package may certainly be useful. Most of my work
is in MATLAB/VC but had to switch over to JAVA for Interfacing few
modules.

Since it has not been explicitly mentioned, I will point out
that Java is a noun - a 'proper name' rather than an acronym
or abbreviation. So 'JAVA' is wrong, whereas 'Java' is correct.

Please (if you intend saying it often) get it correct.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top