Java 以不同方法重用FileWriter时,在何处关闭它?
我遇到了一个问题:我需要能够在动态添加到阵列(dock)并从阵列中移除(undock)后进行文件写入。但是我不知道把flush()和close()放在哪里。当我在写函数之后将其放在任何地方时,我会收到错误,因为它们已经关闭了filewriter。你能帮忙吗Java 以不同方法重用FileWriter时,在何处关闭它?,java,oop,for-loop,if-statement,filewriter,Java,Oop,For Loop,If Statement,Filewriter,我遇到了一个问题:我需要能够在动态添加到阵列(dock)并从阵列中移除(undock)后进行文件写入。但是我不知道把flush()和close()放在哪里。当我在写函数之后将其放在任何地方时,我会收到错误,因为它们已经关闭了filewriter。你能帮忙吗 try { portLog.flush(); } catch (IOException e) { e.printStackTrace(); } try { portLog.close(); } catch (IOE
try {
portLog.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
portLog.close();
} catch (IOException e) {
e.printStackTrace();
}
这是我的密码:
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class Main {
static Scanner scan = new Scanner(System.in);
private static Ship[] dock1 = new Ship[10];
private static Ship[] waitingList = new Ship[10];
static FileWriter portLog;
static DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
//get current date time with Date()
static Date date = new Date();
static {
try {
portLog = new FileWriter("\\Users\\Smith\\Desktop\\PortLog.txt", true);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
menu();
}
public static void menu() {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("Choose an option: 1-3");
System.out.println("1. Dock");
System.out.println("2. Undock");
System.out.println("3. Status");
int menu = scan.nextInt();
switch (menu) {
case 1:
System.out.println("1. Dock");
dock();
break;
case 2:
System.out.println("2. Undock");
undock();
break;
case 3:
System.out.println("3. Status");
printDock();
printWaitingList();
break;
case 4:
System.out.println("4. Exit");
System.exit(0);
default:
System.out.println("No such option");
break;
}
}
}
public static void dock() {
System.out.println("Enter ship's name: ");
String name = scan.nextLine();
System.out.println("Enter ship's size: ");
String size = scan.nextLine();
System.out.println("Enter the ships dock:");
//Check if the dock number is valid
int i = Integer.valueOf(scan.nextLine());
if (i >= 0 && i < 10 && dock1[i] == null) {
int c = 0;
int co = 0;
int sco = 0;
for (int j = 0; j < dock1.length; j++) {
if (dock1[j] != null && dock1[j].getShipSize().equals("Cargo")) {
c++;
}
if (dock1[j] != null && dock1[j].getShipSize().equals("Container")) {
co++;
}
if (dock1[j] != null && dock1[j].getShipSize().equals("Super-Container")) {
sco++;
}
}
if (c < 10 && co < 5 && sco < 2) {
//Add ship to the dock
dock1[i] = new Ship(name, size);
System.out.println("Enough space you can dock");
System.out.println("Ship has been docked");
try {
portLog.write("\n" + " Docked: " + dock1[i].getShipName() + " Size: " + dock1[i].getShipSize() + " at " + dateFormat.format(date));
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("You cannot dock");
waitingList(name, size);
}
} else {
System.out.println("Couldn't dock");
waitingList(name, size);
}
}
public static void undock() {
System.out.println("Status of ships: ");
printDock();
System.out.println("Enter ship's name to undock: ");
String name = scan.nextLine();
for (int i = 0; i < dock1.length; i++) {
if (dock1[i] != null && dock1[i].getShipName().equals(name)) {
try {
portLog.write("\n" + "Undocked: " + dock1[i].getShipName() + " Size: " + dock1[i].getShipSize() + " at " + dateFormat.format(date));
} catch (IOException e) {
e.printStackTrace();
}
dock1[i] = null;
System.out.println("Ship removed");
/// HERE CHECK IF SHIP IN DOCK
for (int j = 0; j < waitingList.length; j++) {
if (dock1[i] == null && waitingList[j] != null) {
// Add ship to the dock
dock1[i] = new Ship(waitingList[j].getShipName(), waitingList[j].getShipSize());
System.out.println("Move ship from waiting list to dock 1");
waitingList[j] = null;
return;
} else {
return;
}
}
} else {
}
}
System.out.println("Ship not found");
}
public static void waitingList(String name, String size) {
System.out.println("Dock 1 is full, ship will try to be added to Waiting List");
for (int i = 0; i < waitingList.length; i++) {
if (waitingList[i] == null) {
//Add ship to the dock
waitingList[i] = new Ship(name, size);
System.out.println("Enough space added to waiting list");
return;
} else {
}
}
System.out.println("No space on waiting list, ship turned away.");
}
public static void printDock() {
System.out.println("Docks:");
for (int i = 0; i < dock1.length; i++) {
if (dock1[i] == null) {
System.out.println("Dock " + i + " is empty");
} else {
System.out.println("Dock " + i + ": " + dock1[i].getShipName() + " " + dock1[i].getShipSize());
}
}
}
private static void printWaitingList() {
System.out.println("Waiting List:");
for (int i = 0; i < waitingList.length; i++) {
if (waitingList[i] == null) {
System.out.println("Dock " + i + " is empty");
} else {
System.out.println("Dock " + i + ": " + waitingList[i].getShipName() + " " + waitingList[i].getShipSize());
}
}
}
}
导入java.io.FileWriter;
导入java.io.IOException;
导入java.text.DateFormat;
导入java.text.simpleDataFormat;
导入java.util.*;
公共班机{
静态扫描仪扫描=新扫描仪(System.in);
私人静态船舶[]dock1=新船[10];
私人静态船舶[]等待列表=新船舶[10];
静态FileWriter端口日志;
静态DateFormat DateFormat=新的SimpleDateFormat(“dd/MM/yyyy HH:MM:ss”);
//使用日期()获取当前日期时间
静态日期=新日期();
静止的{
试一试{
portLog=newfilewriter(“\\Users\\Smith\\Desktop\\portLog.txt”,true);
}捕获(IOE异常){
e、 printStackTrace();
}
}
公共静态void main(字符串[]args){
菜单();
}
公共静态无效菜单(){
扫描仪扫描=新扫描仪(System.in);
while(true){
System.out.println(“选择一个选项:1-3”);
System.out.println(“1.Dock”);
System.out.println(“2.disock”);
系统输出打印项次(“3.状态”);
int menu=scan.nextInt();
开关(菜单){
案例1:
System.out.println(“1.Dock”);
dock();
打破
案例2:
System.out.println(“2.disock”);
解开();
打破
案例3:
系统输出打印项次(“3.状态”);
printDock();
printWaitingList();
打破
案例4:
System.out.println(“4.Exit”);
系统出口(0);
违约:
System.out.println(“无此选项”);
打破
}
}
}
公共静态void dock(){
System.out.println(“输入船名:”);
字符串名称=scan.nextLine();
System.out.println(“输入船舶尺寸:”);
字符串大小=scan.nextLine();
System.out.println(“进入船舶码头:”);
//检查船坞号是否有效
int i=Integer.valueOf(scan.nextLine());
如果(i>=0&&i<10&&dock1[i]==null){
int c=0;
int co=0;
int sco=0;
对于(int j=0;jpublic static void menu() {
Scanner scan = new Scanner(System.in);
boolean keepProcessing = true; // use this to control the loop, don't call System.exit!
// use try-with-resources to control resource lifetime
try (FileWriter portLog = new FileWriter("\\Users\\Smith\\Desktop\\PortLog.txt", true)) {
while (keepProcessing) {
int choice = scan.nextInt();
switch (choice) {
case 1:
System.out.println("1. Dock");
dock(portLog);
break;
// Other cases skipped for brevity
case 4:
keepProcessing = false;
break;
// Other cases skipped for brevity
}
}
}
}
public static void dock(FileWriter portLog) {
// ...
}