有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

我需要java方面的帮助

在代码中,我从输入文件中读取信息并将其写入输出文件。在输入文件中,我有以下信息1245:我的地址:佛罗里达州迈阿密

然后,当信息写入输出时,我需要它来更改:for |。谁能解释一下我该怎么做

import java.io.*;


public class fileConverted {

public static void main(String args[]) {        
    try {
        File input = new File("input");
        File output = new File("output");
        Scanner sc = new Scanner(input);   
        PrintWriter printer = new PrintWriter(output);
        while (sc.hasNextLine()) {
            String s = sc.nextLine();
            printer.write(s);
        }
        printer.flush();
    }
    catch (FileNotFoundException e) {
        System.err.println("File not found. Please scan in new file.");
    }

}

}

共 (4) 个答案

  1. # 1 楼答案

    请试试这个,它很好用

    import java.io.*;
    import java.util.*;
    class fileConverted 
    {
         public static void main(String args[]) 
         {        
             try 
             {
                 File input = new File("input.txt");
                 File output = new File("output.txt");
                 Scanner sc = new Scanner(input);   
                 PrintWriter printer = new PrintWriter(output);
                 while (sc.hasNextLine()) 
                 {
                        String s = sc.nextLine();
                        printer.write(s.replace(':', '|'));
                     }
                 printer.flush();
              }
              catch (FileNotFoundException e) 
          {
                System.err.println("File not found. Please scan in new file.");
              }
        }
    }
    
  2. # 2 楼答案

    char x=f.read();
    if(x==':')
    x='|';
    
  3. # 3 楼答案

    在写入输出文件之前调用字符串。replaceAll()方法

  4. # 4 楼答案

    添加以下行:

    s = s.replace(":", "|");
    

    之后

    String s = sc.nextLine();