not retrieving all the objects while deserializing a file - java

Serializing data through
try {
FileOutputStream fileOut = new FileOutputStream(
"C:\\Users\\saikiran\\Documents\\NetBeansProjects\\FTP\\reg.ser", true);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(r);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/reg.ser");
pr.println("Registered Successfully ");
} catch (IOException i) {
i.printStackTrace();
}
and while Deserializing not getting entire file objects only getting single object i.e starting object only .
FileInputStream fileIn = new FileInputStream("C:\\Users\\saikiran\\Documents\\NetBeansProjects\\FTP\\reg.ser");
ObjectInputStream in = null;
while (fileIn.available() != 0) {
in = new ObjectInputStream(fileIn);
while (in != null && in.available() != 0) {
r = (Registration) in.readObject();
System.out.println("Logged in :" + "User name :" + r.u + "Password " + r.p);
if (r.u.equals(ur) && r.p.equals(ps)) {
System.out.println("Logged in :" + "User name :" + r.u + "Password " + r.p);
pr.println("Display");
}
}
}

I have created the working sample for you .
My POJO serializable class will be ,
import java.io.Serializable;
public class Pojo implements Serializable{
String name;
String age;
String qualification;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getQualification() {
return qualification;
}
public void setQualification(String qualification) {
this.qualification = qualification;
}
}
My main class will be,
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class Serialization {
/**
* #param args
*/
public static final String FILENAME = "F:\\test\\cool_file.ser";
public static void main(String[] args) throws IOException, ClassNotFoundException {
FileOutputStream fos = null;
//ObjectOutputStream oos = null;
try {
fos = new FileOutputStream(FILENAME);
//oos = new ObjectOutputStream(fos);
/* for (String s : test.split("\\s+")) {
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(s);
}*/
for(int i=0;i<10;i++){
ObjectOutputStream oos = new ObjectOutputStream(fos);
Pojo pojo = new Pojo();
pojo.setName("HumanBeing - "+i);
pojo.setAge("25 - "+i);
pojo.setQualification("B.E - "+i);
oos.writeObject(pojo);
}
} finally {
if (fos != null)
fos.close();
}
List<Object> results = new ArrayList<Object>();
FileInputStream fis = null;
//ObjectInputStream ois = null;
try {
fis = new FileInputStream(FILENAME);
//ois = new ObjectInputStream(fis);
while (true) {
ObjectInputStream ois = new ObjectInputStream(fis);
results.add(ois.readObject());
}
} catch (EOFException ignored) {
// as expected
} finally {
if (fis != null)
fis.close();
}
System.out.println("results = " + results);
for (int i=0; i<results.size()-1; i++) {
System.out.println(((Pojo)results.get(i)).getName()+ " "+((Pojo)results.get(i)).getAge()+ " "+((Pojo)results.get(i)).getQualification());
}
}
}
Hope it helps.

Related

Using ObjectStreams to send Lists over sockets

I need to transfer a List between nodes and replace the existing one with the new one in the new node to achieve this, I'm using Sockets from Java.
I somehow have managed to transfer the data but only when I terminate the process. I need it to continue running, the process but at the same time transfer, the data in case any other new node joins the List.
How can I achieve this? I will have to introduce Threads in the Download along the road.
I got it working with files but now I need to change it to Sync lists, just having this is enough?
private static List<CloudByte> cloudByteList = Collections.synchronizedList(new ArrayList<>());
This is my current code:
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static FileData.getCloudByteList;
import static FileData.getFile;
public class FileData {
private static File file;
private static String fileName;
private static List<CloudByte> cloudByteList = Collections.synchronizedList(new ArrayList<>());
public FileData(String fileName) throws IOException {
if (fileName == null) {
this.fileName = "data2.bin";
this.file = new File(this.fileName);
Download.downloadFile();
} else {
this.file = new File(fileName);
this.fileName = fileName;
fillingList();
}
}
public void fillingList() throws IOException {
byte[] fileContents = Files.readAllBytes(file.toPath());
for (int i = 0; i < fileContents.length - 1; i++) {
cloudByteList.add(new CloudByte(fileContents[i]));
}
}
public static List<CloudByte> getCloudByteList() {
return cloudByteList;
}
public static File getFile() {
return file;
}
public String getFileName() {
return fileName;
}
public static void setFile(File file) {
FileData.file = file;
}
/*--------------------------Download--------------------------*/
}
class Download extends Thread {
static ConnectingDirectory connectingDirectory;
#Override
public void run() {
try {
downloadFile();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void downloadFile() throws IOException {
var nodes = ConnectingDirectory.getNodes();
Socket socket = null;
if (getFile().exists()) {
System.out.println("File: " + getFile() + " exists.");
new Upload().uploadFile();
}
FileOutputStream fos = new FileOutputStream(FileData.getFile());
//ObjectOutputStream oos = new ObjectOutputStream(fos);
for (int i = 0; i < nodes.size() - 1; i++) {
if (!(nodes.get(i).getHostPort() == ConnectingDirectory.getHostIP())) {
System.out.println("test33123");
ServerSocket serverSocket = new ServerSocket(nodes.get(i).getHostPort());
System.out.println(serverSocket);
socket = serverSocket.accept();
System.out.println("now socket");
System.out.println(socket);
//socket = new Socket(nodes.get(i).getName(), nodes.get(i).getHostPort());
//System.out.println(socket);
int bytes = 0;
DataInputStream ois = new DataInputStream(socket.getInputStream());
long size = ois.readLong();
System.out.println(size);
byte[] buffer = new byte[100 * 10000];
while (size > 0 && (bytes = ois.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
System.out.println("test3333");
fos.write(buffer, 0, bytes);
size -= bytes;
}
}
}
}
}
/*--------------------------Upload--------------------------*/
class Upload {
public void uploadFile() throws IOException {
int bytes = 0;
var nodes = ConnectingDirectory.getNodes();
FileInputStream fileInputStream = new FileInputStream("data.bin");
DataInputStream ois = new DataInputStream(fileInputStream);
if (!getFile().exists()) {
System.out.println("File doesn't exist." + "\nDownloading the file!");
new Download().downloadFile();
}
System.out.println("hello");
for (int i = 0; i < nodes.size() - 1; i++) {
System.out.println("hello2");
Socket socket = new Socket(nodes.get(i).getName(), nodes.get(i).getHostPort());
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeLong(new File("data.bin").length());
byte[] buffer = new byte[100 * 10000];
while ((bytes = ois.read(buffer)) != -1) {
dos.write(buffer, 0, bytes);
dos.flush();
}
}
}
}
As you can see, I'm using DataInput because if I try to use the ObjectInputStream, I get a Corrupted Header Exception. I have more classes to add to this. My goal is as I said, to transfer the data inside the "data.bin" to a "data2.bin" file. I'm able to create it and delete it but at the same time, no Data is being written/sent to it.
How can I fix the CorruptedHeaderException and get it to send the content?
All help is appreciated.
StorageNode Class:
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Pattern;
import static FileData.*;
public class StorageNode extends Thread {
private static int serverPort = 8080;
private static int clientPort = 8082;
private static String fileName = null;
private static String addressName = "localhost";
private static ConnectingDirectory connectingDirectory;
private static FileData fileData;
static ErrorInjection errorInjection;
public static void main(String[] args) throws IOException, InterruptedException {
/* if (args.length > 3) {
addressName = args[0];
serverPort = Integer.parseInt(args[1]);
clientPort = Integer.parseInt(args[2]);
fileData = new FileData(args[3]);
} else {
fileName = null;
fileData = new FileData(fileName);
}*/
connectingDirectory = new ConnectingDirectory(addressName, clientPort, serverPort);
fileData = new FileData(fileName);
errorInjection = new ErrorInjection();
errorInjection.start();
if(fileData.getFile().exists()){
new Upload().uploadFile();
}else {
new Download().downloadFile();
}
}
ConnectingDirectory Class
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ConnectingDirectory {
private String hostName;
private static int hostIP;
private int directoryIP;
private InetAddress address;
private InputStream in;
private OutputStream out;
private static List<Nodes> nodes = new ArrayList<>();
List<String> nodess = new ArrayList<>();
private Socket socket;
private String sign = "INSC ";
public ConnectingDirectory(String hostName, int hostIP, int directoryIP) throws IOException {
this.hostName = hostName;
this.hostIP = hostIP;
this.directoryIP = directoryIP;
this.address = InetAddress.getByName(hostName);
this.socket = new Socket(address, directoryIP);
signUp();
askConnectedNodes();
}
public void signUp() throws IOException {
System.out.println("You are connecting to the following address: " + hostIP + "\n");
System.out.println("The port you are connected to: " + socket.getPort() + "\n");
in = socket.getInputStream();
out = socket.getOutputStream();
out.write(generateSignUp(address, hostIP).getBytes());
out.flush();
}
public String generateSignUp(InetAddress address, int hostIP) {
String signUpString = sign + address + " " + hostIP + "\n";
return signUpString;
}
public void askConnectedNodes() throws IOException {
String directoryNodesAvailable;
String a = "nodes\n";
out.write(a.getBytes());
out.flush();
Scanner scan = new Scanner(in);
while (true) {
directoryNodesAvailable = scan.nextLine();
addExistingNodes(directoryNodesAvailable);
//System.out.println("Eco: " + directoryNodesAvailable);
if (directoryNodesAvailable.equals("end")) {
out.flush();
printNodes();
break;
}
}
}
public void addExistingNodes(String sta) throws IOException {
if (sta.equals("end")) return;
if (!(nodess.contains(sta))) {
nodess.add(sta);
nodes.add(new Nodes(nodess.get(nodess.size() - 1)));
}
return;
}
public static List<Nodes> getNodes() {
return nodes;
}
public void printNodes() {
System.out.println("Checking for available nodes: \n");
nodes.forEach((z) -> System.out.println(z.getNode()));
}
public Socket getSocket() {
return socket;
}
public static int getHostIP() {
return hostIP;
}
public InetAddress getAddress() {
return address;
}
}
For all of those that need help in the future:
Sender side:
Socket socket = new Socket("localhost", hostPort);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
ByteBlockRequest bbr = new ByteBlockRequest(getStoredData());
objectOutputStream.writeObject(bbr.blocksToSend(j));
Receiver side:
Socket = StorageNode.getServerSocket().accept();
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
byte[] bit = (byte[]) ois.readObject();
In my case, I needed to use byte[], so I had to do a few additional functions in the back, to change Cloudbyte[] into byte[]. Once I did that, I was able to send the data using, ObjectInput/ObjectOutput.

How can i fix this java.io.NotSerializableException: error in java?

I'm quite new to java and unsure how to fix this java.io.NotSerializableException error.
I'm trying to use an add button on the GUI to add an object to an array list
Then write that object to a file so I am able to read it back.
Here is the code I'm using for the Branch class which implement Java Serializable:
import java.io.Serializable;
public class Branch implements Serializable{
private String branch_name;
private String branch_address;
public Branch(String Bname, String Baddress) {
this.branch_name = Bname;
this.branch_address = Baddress;
public String getbranch_name(){
return branch_name;
}
public String getbranch_address(){
return branch_address;
}
public void show_branch_details() {
System.out.println( " The branch name is : " + getbranch_name()
+ " branch address :"+ getbranch_address()
}
}
}
Here is the code for the add button:
ArrayList<Branch> BranchList = new ArrayList<Branch>();
JButton AddBranch = new JButton("ADD BRANCH");
AddBranch.setBounds(10, 35, 161, 23);
AddBranch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Bname = branchNameField.getText();
String Baddress = branchAddressField.getText();
Branch A = new Branch(Bname, Baddress);
BranchList.add(A);
for (int i = 0; i < BranchList.size(); i++) {
displayInfo.append(BranchList.get(i).getbranch_name() +);
}
System.out.println("The ArrayList has " + BranchList.size());
for (int i = 0; i < BranchList.size(); i++) {
System.out.println(BranchList.get(i).getbranch_name());
}
try {
FileOutputStream fos = new FileOutputStream("branch.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//oos.writeObject(BranchList);
for (int b = 0; b < BranchList.size(); b++) {
oos.writeObject(BranchList.get(b));
}
oos.flush();
oos.close();
FileInputStream fis = new FileInputStream("branch.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
//BranchList = (ArrayList<Branch>)ois.readObject();
Branch obj = null;
while ((obj = (Branch) ois.readObject()) != null) {
System.out.println("Name:" + obj.getbranch_name() + ", Address:"
+ obj.getbranch_address());
}
ois.close();
} catch (IOException ex) {
System.out.println(" IOE ERROR");
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
System.out.println("class ERROR");
ex.printStackTrace();
}
}
});
Please use this as Branch class :
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
class Branch implements Serializable{
private String branch_name;
private String branch_address;
public Branch(String Bname, String Baddress) {
this.branch_name = Bname;
this.branch_address = Baddress;
}
public String getbranch_name(){
return branch_name;
}
public String getbranch_address(){
return branch_address;
}
public void show_branch_details() {
System.out.println( " The branch name is : " + getbranch_name()
+ " branch address :"+ getbranch_address());
}
}
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Branch A= new Branch("TestA","Add_A");
Branch B= new Branch("TestB","Add_B");
ArrayList<Branch> BranchList = new ArrayList<>();
BranchList.add(A);
BranchList.add(B);
FileOutputStream fos = new FileOutputStream("branch.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(BranchList);
oos.flush();
oos.close();
ArrayList<Branch> OutputBranchList = new ArrayList<>();
FileInputStream fis = new FileInputStream("branch.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
OutputBranchList = (ArrayList) ois.readObject();
for(Branch branch : OutputBranchList) {
System.out.println(branch.getbranch_name()+" "+ branch.getbranch_address());
}
ois.close();
}
}

Sometime when i Deserialize it wont load the ArrayList java

I'm saving my ArrayList by Serializing it:
public void Save(){
try{
FileOutputStream ficheiro = new FileOutputStream("Gravacao");
ObjectOutputStream out = new ObjectOutputStream(ficheiro);
out.writeObject(ClassListas.ListaCliente);
out.writeObject(ClassListas.ListaFornecedor);
out.writeObject(ClassListas.ListaPessoa);
out.writeObject(ClassListas.ListaStocks);
out.writeObject(ClassListas.ListaVenda);
out.writeObject(ClassListas.ListaRecurso);
out.flush();
out.close();
ficheiro.close();
}catch(IOException e){
e.printStackTrace();
}
}
And I deserialize it by:
public class FormPrincipal extends javax.swing.JFrame {
public FormPrincipal() {
initComponents();
try{
FileInputStream fx = new FileInputStream("Gravacao");
ObjectInputStream in = new ObjectInputStream(fx);
ClassListas.ListaCliente = (ArrayList<ClassCliente>) in.readObject();
ClassListas.ListaFornecedor = (ArrayList<ClassFornecedor>) in.readObject();
ClassListas.ListaPessoa = (ArrayList<ClassPessoa>) in.readObject();
ClassListas.ListaRecurso = (ArrayList<ClassRecurso>) in.readObject();
ClassListas.ListaStocks = (ArrayList<ClassStock>) in.readObject();
ClassListas.ListaVenda = (ArrayList<ClassVenda>) in.readObject();
}catch(Exception e){
e.printStackTrace();
}
}
And this is how i have my ArrayLists declared:
import java.util.ArrayList;
import main.ClassPessoa;
import main.ClassCliente;
import main.ClassFornecedor;
import main.ClassStock;
import main.ClassVenda;
import main.ClassRecurso;
import java.io.Serializable;
/**
*
* #author Skray
*/
public class ClassListas implements Serializable {
//Listas
public static ArrayList<ClassPessoa> ListaPessoa = new ArrayList<ClassPessoa>();
public static ArrayList<ClassCliente> ListaCliente = new ArrayList<ClassCliente>();
public static ArrayList<ClassFornecedor> ListaFornecedor = new ArrayList<ClassFornecedor>();
public static ArrayList<ClassStock> ListaStocks = new ArrayList<ClassStock>();
public static ArrayList<ClassVenda> ListaVenda = new ArrayList<ClassVenda>();
public static ArrayList<ClassRecurso> ListaRecurso = new ArrayList<ClassRecurso>();
}
The problem is, sometimes, when I Deserialize this it won't load to ArrayList, but when I close and open again it Deserializes... Can someone explain me why?
First of all you can write your ClassListas instance instead of reading and writing each and every ArrayList seperately using following line
ClassListas.ListaCliente = (ArrayList<ClassCliente>) in.readObject();
ClassListas.ListaFornecedor = (ArrayList<ClassFornecedor>) in.readObject();
ClassListas.ListaPessoa = (ArrayList<ClassPessoa>) in.readObject();
ClassListas.ListaRecurso = (ArrayList<ClassRecurso>) in.readObject();
ClassListas.ListaStocks = (ArrayList<ClassStock>) in.readObject();
ClassListas.ListaVenda = (ArrayList<ClassVenda>) in.readObject();
out.writeObject(ClassListas.ListaCliente);
out.writeObject(ClassListas.ListaFornecedor);
out.writeObject(ClassListas.ListaPessoa);
out.writeObject(ClassListas.ListaStocks);
out.writeObject(ClassListas.ListaVenda);
out.writeObject(ClassListas.ListaRecurso);
Then the object is loaded from file during component initialisation only you can move code to separate method
public ClassListas readFromFile( String file) {
ClassListas classListas = null ;
try{
FileInputStream fx = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fx);
classListas = (ClassListas) in.readObject();
}catch(Exception e){
e.printStackTrace();
} finally {
if( fx != null )
fx.close();
}
return classListas;
}
public void writeToFile( String file,ClassListas classListas ) {
try{
FileOutputStream ficheiro = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(ficheiro);
out.writeObject(classListas);
out.flush();
out.close();
} catch(IOException e){
e.printStackTrace();
} finally {
if( ficheiro != null )
ficheiro.close();
}
}
You can use call readFromFile and writeToFile in Load and save button of the form.

Specify where to add file after zip

I am new to java and I am having trouble specifying where to save the folder after I zipped it. It always go to the project workspace that I am creating. I want it to be saved in the path
"C:\\Users\\win8.1\\Desktop\\AES"
Here is the code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtils
{
private List<String> fileList;
private static final String OUTPUT_ZIP_FILE = "Folder.zip";
private static final String SOURCE_FOLDER = "C:\\Users\\win8.1\\Desktop\\AES\\SAMPLEPATH"; // SourceFolder path
public ZipUtils()
{
fileList = new ArrayList<String>();
}
public static void main(String[] args)
{
ZipUtils appZip = new ZipUtils();
appZip.generateFileList(new File(SOURCE_FOLDER));
appZip.zipIt(OUTPUT_ZIP_FILE);
}
public void zipIt(String zipFile)
{
byte[] buffer = new byte[1024];
String source = "";
FileOutputStream fos = null;
ZipOutputStream zos = null;
try
{
try
{
source = SOURCE_FOLDER.substring(SOURCE_FOLDER.lastIndexOf("\\") + 1, SOURCE_FOLDER.length());
}
catch (Exception e)
{
source = SOURCE_FOLDER;
}
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(fos);
System.out.println("Output to Zip : " + zipFile);
FileInputStream in = null;
for (String file : this.fileList)
{
System.out.println("File Added : " + file);
ZipEntry ze = new ZipEntry(source + File.separator + file);
zos.putNextEntry(ze);
try
{
in = new FileInputStream(SOURCE_FOLDER + File.separator + file);
int len;
while ((len = in.read(buffer)) > 0)
{
zos.write(buffer, 0, len);
}
}
finally
{
in.close();
}
}
zos.closeEntry();
System.out.println("Folder successfully compressed");
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
try
{
zos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public void generateFileList(File node)
{
// add file only
if (node.isFile())
{
fileList.add(generateZipEntry(node.toString()));
}
if (node.isDirectory())
{
String[] subNote = node.list();
for (String filename : subNote)
{
generateFileList(new File(node, filename));
}
}
}
private String generateZipEntry(String file)
{
return file.substring(SOURCE_FOLDER.length() + 1, file.length());
}
}
I got the code here. Thank you in advance!
There is no problem. Just write
private static final String OUTPUT_ZIP_FILE = "C:\\Users\\win8.1\\Desktop\\AES\\Folder.zip";
private static final String SOURCE_FOLDER = "C:\\Users\\win8.1\\Desktop\\AES\\SAMPLEPATH";

writing an arraylist to file

I'm having some trouble writing an array list to file using printwriter. I've tried another way that worked but it wouldn't print all the things from the array list just one. This is the way I'm trying at the moment and it won't print anything.
datArrayList = new ArrayList<theAccounts>();
File file = new File("output.txt");
public void writer() throws FileNotFoundException, IOException{
PrintWriter pw = new PrintWriter(new FileOutputStream(file));
FileOutputStream fo = new FileOutputStream(file);
int datList = datArrayList.size();
for (int i = 0; i < datList; i++){
pw.write(datArrayList.get(i).toString() + "\n");
}
Can anyone tell me what i should be doing to write all the items in the array to the output file? thank you :)
datArrayList = new ArrayList<theAccounts>();
File file = new File("output.txt");
public void writer() throws FileNotFoundException, IOException {
FileOutputStream fo = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fo);
int datList = datArrayList.size();
for (theAccounts elem : datArrayList){
pw.println(elem);
}
pw.close();
fo.close();
}
Possibly because you weren't closing your streams, try:
datArrayList = new ArrayList<theAccounts>();
File file = new File("output.txt");
public void writer() throws FileNotFoundException, IOException {
try(PrintWriter pw = new PrintWriter(new FileOutputStream(file))){
int datList = datArrayList.size();
for (theAccounts s : datArrayList){
pw.println(s);
}
}
}
Here's the code that works. Need to flush/close streams in finally block.
package com.sto.sanbox;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class Accounts {
public class Account {
String name;
String amount;
public Account(String name, String amount) {
super();
this.name = name;
this.amount = amount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String toString() {
return this.getName() + ", " + this.getAmount();
}
}
public void writer(ArrayList<Account> datArrayList) throws IOException {
PrintWriter pw = null;
FileOutputStream fo = null;
File file = null;
try {
file = new File("output.txt");
pw = new PrintWriter(new FileOutputStream(file));
fo = new FileOutputStream(file);
int datList = datArrayList.size();
for (int i = 0; i < datList; i++) {
pw.write(datArrayList.get(i).toString() + "\n");
}
} finally {
pw.flush();
pw.close();
fo.close();
}
}
public static void main(String args[]) {
Accounts Writer = new Accounts();
ArrayList<Account> datArrayList = new ArrayList<Account>();
Account account = Writer.new Account(" Name" , " 100000");
datArrayList.add(account);
try {
Writer.writer(datArrayList);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Some things to consider:
Closing your PrintWriter
Not making a second FileOutputStream called fo since this is unneeded
Making sure you're creating your file output.txt via file.newFile()

Categories