0
オブジェクトを直接書き込んで、次のテストファイルを実装するJavaプログラムをテストしていますが、残念ながら未知の文字でファイルを取得しています。また、あなたがそれで、あなたの意志ジャンク文字としてファイルにオブジェクトをシリアル化した場合、プレーンテキスト形式でその属性を持つオブジェクトが含まれており、出力は空のファイルを返し、別のファイル..出力テキストファイルにオブジェクトを書き込む
import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
public class AssignTest {
public static void main(String[] args) throws IOException {
FileOutputStream wo = new FileOutputStream("Assignment1.txt");//here we want the objects directly
ObjectOutputStream s = new ObjectOutputStream(wo);
FileWriter fw = new FileWriter("yami.txt");//while here we want the object with its attributes
PrintWriter pw = new PrintWriter(fw);
String fileName= "Assignment1.txt";
try{
ArrayList<Worker> Worker1 = new ArrayList<Worker>();
Worker1.add(new SalariedWorker());// index 0
SalariedWorker sw = (SalariedWorker) Worker1.get(0);//explicit casting
Worker1.add(new SalariedWorker(1000.0));// index 1
SalariedWorker sw1 = (SalariedWorker) Worker1.get(1);
Worker1.add(new HourlyWorker());// index 2
HourlyWorker hw = (HourlyWorker) Worker1.get(2);
Worker1.add(new HourlyWorker(100.0));// index 3
HourlyWorker hw1 = (HourlyWorker) Worker1.get(3);
Scanner prompt = new Scanner (System.in);
System.out.println("Enter your monthly salary: ");
double monthlySalary = prompt.nextDouble();
sw.setSalary(monthlySalary);
System.out.println("Your monthly Salary is: " +monthlySalary+" AED");
System.out.println(sw.toString());
System.out.println("Enter the Percentage Increase for a Salaried worker: ");
double PercentIncreaseS = prompt.nextDouble();
sw.setpercentageIncrease(PercentIncreaseS);
sw.increasePayment(monthlySalary);
System.out.println(sw.toString(monthlySalary));
System.out.println("Enter your hourly rate: ");
double HourlyRate = prompt.nextDouble();
hw.setRate(HourlyRate);
System.out.println("Your Hourly Rate : "+HourlyRate+" AED");
System.out.println(sw.toString());
System.out.println("Enter the Percentage Increase for an Hourly worker: ");
double PercentIncreaseH = prompt.nextDouble();
hw.setpercentageIncrease(PercentIncreaseH);
hw.increasePayment(HourlyRate);
System.out.println(hw.toString(HourlyRate));
ObjectOutputStream os = new ObjectOutputStream (new FileOutputStream(fileName));
os.close();
s.writeObject(sw);
s.writeObject(hw);
pw.print(sw);
pw.print(hw);
}
catch (ArithmeticException ax){
System.out.println(ax);
}
catch (IOException ex){
System.out.println(ex);
}
}
}
標準入力に送信する必要があるかどうかを実行する方法については、[最小、完全、および検証可能な例](http://stackoverflow.com/help/mcve)をご提供ください。あなたが期待している結果とあなたが受けている結果の結果と差異。それはコミュニティが問題とそれに対処する方法を理解するのに役立ちます。 – yeputons