This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

To export an array to a folder .txt  RSS feed

 
alfonso alarcon
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
 
alfonso alarcon
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
}
}

}
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!