You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中使用PrintWriter写入文件问题求助(File I/O新手)

Hey there! Let's tackle your PrintWriter issue for saving usernames and passwords. I notice your current code doesn't include the PrintWriter implementation yet, so I'll walk you through how to add it properly, plus point out common pitfalls to avoid.

How to Use PrintWriter to Save User Credentials

First, let's update your code to handle user input and write to a file. Here's a working, revised version:

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Helper { 
    public static void main(String[] args) throws Exception { 
        Home(); 
        UserInput(); 
    } 

    private static void Home() { 
        System.out.println("Here are the instructions for the chat program:"); 
        System.out.println("Type *...");
    }

    private static void UserInput() throws FileNotFoundException {
        Scanner scanner = new Scanner(System.in);
        
        // Get username and password from user
        System.out.print("Enter your username: ");
        String username = scanner.nextLine();
        System.out.print("Enter your password: ");
        String password = scanner.nextLine();

        // Use try-with-resources to auto-close the PrintWriter (critical!)
        try (PrintWriter writer = new PrintWriter("user_credentials.txt")) {
            writer.println("Username: " + username);
            writer.println("Password: " + password);
            System.out.println("Credentials saved to file successfully!");
        }
        
        scanner.close();
    }
}

Common Issues & Fixes

  • File Not Saving/Disappearing: If you don't close the PrintWriter, buffered data might never get written to the file. The try-with-resources syntax in the example automatically closes the writer, so you don't have to remember to call writer.close().
  • Overwriting Existing Data: By default, PrintWriter will erase and overwrite an existing file. To append new credentials instead, use this constructor (you'll need to import java.io.FileWriter):
    try (PrintWriter writer = new PrintWriter(new FileWriter("user_credentials.txt", true))) {
        // Your write logic here
    }
    
  • FileNotFoundException: This usually means you don't have write permissions for the directory, or you specified an invalid file path. If you're unsure, use an absolute path (like C:\\Users\\YourName\\Documents\\user_credentials.txt on Windows or /home/YourName/user_credentials.txt on Linux/Mac).
  • Security Warning: Storing plain-text passwords is a huge risk! For real-world apps, always hash passwords with a library like BCrypt before saving them to a file.

If you had a specific error or unexpected behavior with your original PrintWriter code, share those details and I can help troubleshoot further!

内容的提问来源于stack exchange,提问作者John Lutz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:54:28