Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › how do I save a 2d array to a text file in java?
New Posts  All Forums:Forum Nav:

how do I save a 2d array to a text file in java?

post #1 of 7
Thread Starter 
Hello again, well im about 80% complete with my program, all I need to do is to
is get get my program to save the values of an array which has dimensions [x][4] doesnt matter how long x is lets just say that it can be as long as I want.

now I dont know how to save the array to a text file which I have in a specific location which I will need to use later on.

thanks for any help you guys can provide.
Shironeko
(17 items)
 
   
CPUMotherboardGraphicsRAM
i7 860 EVGA P55 SLI EVGA GTX 460 EE 16GB Corsair Vengence CL9 1.5v 
Hard DriveOptical DriveCoolingOS
1xWD 320GB, 2xWD 1TB raid 0, 2x WD 2TB Sata DVD burner Prolimatech Megahalem Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
22" Aoc 2217v 10000:1 5ms G15 V2 Corsair TX750 Lian Li V1000 plus (modded) 
MouseMouse PadAudioAudio
Razer Lachesis V2 Razer Ironclad Razer Orca Headphones Asus Xonar Essence ST (to be modded) 
Audio
Corsair SP2500 2.1 
CPUMotherboardGraphicsRAM
Intel Core 2 duo SL9400 @1.6ghz 1v Lenovo x200T Intel GMA4500x 4GB Samsung 7-7-7-20 1066mhz DDR3 
Hard DriveCoolingOSMonitor
Hitachi 160GB stock Windows 7 Ultimate x86 Resistive Tablet LCD Screen 
PowerCaseMouse
65w Lenovo Power Adapter Lenovo x200T Trackpoint 
CPUGraphicsRAMHard Drive
Dual 2.8Ghz Socket 603/604 Xeons ATI Rage XL 8MB 4GB DDR 266Mhz 6x 36gb 10k rpm SCSI 
CoolingCase
Stock Poweredge 2600 case 
  hide details  
Reply
Shironeko
(17 items)
 
   
CPUMotherboardGraphicsRAM
i7 860 EVGA P55 SLI EVGA GTX 460 EE 16GB Corsair Vengence CL9 1.5v 
Hard DriveOptical DriveCoolingOS
1xWD 320GB, 2xWD 1TB raid 0, 2x WD 2TB Sata DVD burner Prolimatech Megahalem Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
22" Aoc 2217v 10000:1 5ms G15 V2 Corsair TX750 Lian Li V1000 plus (modded) 
MouseMouse PadAudioAudio
Razer Lachesis V2 Razer Ironclad Razer Orca Headphones Asus Xonar Essence ST (to be modded) 
Audio
Corsair SP2500 2.1 
CPUMotherboardGraphicsRAM
Intel Core 2 duo SL9400 @1.6ghz 1v Lenovo x200T Intel GMA4500x 4GB Samsung 7-7-7-20 1066mhz DDR3 
Hard DriveCoolingOSMonitor
Hitachi 160GB stock Windows 7 Ultimate x86 Resistive Tablet LCD Screen 
PowerCaseMouse
65w Lenovo Power Adapter Lenovo x200T Trackpoint 
CPUGraphicsRAMHard Drive
Dual 2.8Ghz Socket 603/604 Xeons ATI Rage XL 8MB 4GB DDR 266Mhz 6x 36gb 10k rpm SCSI 
CoolingCase
Stock Poweredge 2600 case 
  hide details  
Reply
post #2 of 7
use a for loop with x as a counter and loop the list until x= the number of items in the list. everytime adding to the file
post #3 of 7
as long as your array object implements the Serializable interface then your all set. if not just stick your array in an ArrayList object. Java Serialization encodes/decodes the entire object to a byte stream and reads/writes it to the file.
http://en.wikipedia.org/wiki/Serialization#Java

here's an example of how to write the methods to accomplish serialization.
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #4 of 7
The above mentioned method stores the object in a binary format. You could also serialize to plain text, JSON, and XML. Advantages of these other approaches would be access from non java code or java code that has no knowledge of the object type. Although since it is an array you are talking about, the second shouldn't happen. It is good to understand what exactly it means to serialize an object, and that is simply to convert some data structure to some persistent format.
Edited by xtascox - 6/13/11 at 12:24pm
post #5 of 7
I would use the json lib http://json-lib.sourceforge.net/

or jaxb if you'd rather use pojo<->xml
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
post #6 of 7
Quote:
Originally Posted by lloyd mcclendon;13857724 
I would use the json lib http://json-lib.sourceforge.net/

or jaxb if you'd rather use pojo<->xml

Jaxb is very easy to use. Simply annotate the object and its members. I use it all the time for web service calls and responses.
post #7 of 7
Well I found that entertaining, I did not know how to loop through a 2 dimensional array of x length before, it turned out to be really easy. This method should do what you asked... or at least help you get on the right path.

You would use it like so: writeArrayToFile(stringArray, "myFile.txt");
Code:
/**
* Writes contents of String array to a file
* @param array Array to be written
* @param filename File to be written to
*/
public void writeArrayToFile(String[][] array, String filename){
PrintStream ps;
try {
ps = new PrintStream(new FileOutputStream(filename));
for(int row=0;row < array.length;row++){
for(int col=0; col < array[row].length;col++){
String s = array[row][col];
ps.println(s);
}
}
ps.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › how do I save a 2d array to a text file in java?