Forums Register Login

To export an array to a folder .txt

+Pie Number of slices to send: Send
Hello,

I am creting a program in which a need to export the creted data
in a folder .txt (or .dat). The data are save in an array type double. I need that somebody indicated to me the steps to create this file and the possible problems. You need more information?? It is possible an example code??

thanks!!
+Pie Number of slices to send: Send
For information on how to read and write to files (text or binary), have a look at the I/O lesson in The Java Tutorial.
+Pie Number of slices to send: Send
FileInputStream and FileOutputStream can help you.
+Pie Number of slices to send: Send
Hello,

Thanks to your indications I have create a file .txt. But I cannot save the array in its interior correctly. If you open the file .txt created arise strange caracteres but not the correct values. Can you help me??

Thanks!

The code is the following one

import java.io.*;
public class Prueba1
{
static int DIM=5000;
static double Q=1.6e-19;
static double[] EC = new double[DIM];
static int ANIZ=1800;
static int ande=DIM-ANIZ;
static int BA=10;
static int DEB=60;
static int A1=ANIZ+500;
static int B1=A1+BA;
static int C1=A1+B1;
static int D1=C1+BA;
static double VDS=0.1*Q;
static double PEN;
static double BARRERA=0.30*Q;
static double BARRERA2=-0.15*Q;

static void potencial ()
{
int i;
PEN=VDS/(D1-A1);
for(i=0;i<=A1-1;i++)
{
EC[i]=0;
}


for(i=A1;i<=B1;i++)
{
EC[i]=BARRERA-PEN*(i-A1);


}

for(i=B1+1;i<=C1-1;i++)
{
EC[i]=BARRERA2-PEN*(i-A1);

}

for(i=C1;i<=D1;i++)
{
EC[i]=BARRERA-PEN*(i-A1);


}

for(i=D1+1;i<=DIM-1;i++)
{
EC[i]=-VDS;
}

for(i=C1;i<=D1;i++)
{

}
}
public static void main(String[] args)
{
int i;
Prueba1.potencial();

try
{

RandomAccessFile pot = new RandomAccessFile("E:\\pot.txt","rw");
for(i=0;i<=DIM-1;i++)
{
pot.writeDouble(EC[i]);
}

pot.close();


}
catch(IOException e)


{
System.out.println("Error---"+e.toString());
}
}

}
+Pie Number of slices to send: Send
Have you tried to write your own program that reads the data from this array? You should be able to recreate the whole array from this file if you do it correctly.

It appears that you might not understand how the data is being written to the file. The program will write the data in its binary representation to the file, so you will not be able to open the file in Notepad (or any other text editor) to see the data as text.

With that said, I don't think you should use RandomAccessFile for this sitatuion. Instead, you should look at the FileOutputStream class as indicated above. You should also look at the I/O Stream tutorial that Jesper mentioned (and provided a link for). This tutorial will help you understand the different OutputStream and InputStream classes that are available for you to use and how to link them together.

Good luck!

Layne
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1857 times.
Similar Threads
How to export selected rows of displaytag to a file?
Read .txt file n append lines using shell script
Best way to load data file into an array
JUnit report
How to delete a zipped folder
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:56:27.