有 Java 编程相关的问题?

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

Java:将字符串写入。txt发行

下面是我的程序的较长但有注释的代码:

public static void main(String args[])
{
   //get path of dump file
   Path directory = Paths.get("E:\\Temp\\");
    try
    {
        Files.createDirectory(directory);
    }
    catch(FileAlreadyExistsException e)
    {
        System.err.println("Directory already exists.");
    }
    catch(IOException e)
    {
        System.err.println("Could not create directory.");
        e.printStackTrace();
        System.exit(1);
    }
    Path file = directory.resolve("dump.txt");

    //set initial vars and get system properties
    long gig = 1_073_741_824L;
    String separator = System.lineSeparator();
    FileSystem fs = FileSystems.getDefault();
    Iterable<FileStore> fstores = fs.getFileStores();

    //write data to file
    try(WritableByteChannel wbc = Files.newByteChannel(file, CREATE, WRITE,TRUNCATE_EXISTING))
    {
        //write heading to file
        ByteBuffer buf_1 = ByteBuffer.wrap(new String("*****FILE SYSTEM DATA*****" + separator + separator).getBytes());
        wbc.write(buf_1);

        //write file store information to file
        for(FileStore store : fstores)
        {
            //set up empty string and formatter for it
            String f_string = "";
            Formatter f = new Formatter(f_string);
            //fill f_string
            f.format("\nStore: %-20s Format: %-5s Capacity: %5dGB Unallocated: %5dGB",
                    store.name(), 
                    store.type(), 
                    store.getTotalSpace()/gig, 
                    store.getUnallocatedSpace()/gig);
            //test
            System.out.println(f_string);
            //set up buffers
            ByteBuffer buf = ByteBuffer.wrap(f_string.getBytes());
            //write to file
            wbc.write(buf);     
            f.close();
        }
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
}

本程序的目的是创建一个。txt文件名为“dump.txt”,位于我的系统上(当前)硬编码的位置,包含有关我的文件系统的信息。我遇到的问题是,除了标题“****文件系统数据*******”,没有任何内容写入文件,事实上,当我调试代码时,在创建格式化程序和最终catch块之间的每一行都没有完成。我尝试将此数据写入文件的方式与将buf_1(标题)写入文件的方式相同,因此我完全不知道为什么会发生此问题

如有任何建议,将不胜感激

另外,我曾考虑过使用Writer,但为了我自己的知识,我使用channel/buffer对象。然而,如果你知道为什么一个作家会更优秀,请告诉我:)


共 (2) 个答案

  1. # 1 楼答案

    在将格式化字符串写入文件之前,您没有将其分配给f_string

    而不是

    String f_string = "";
    Formatter f = new Formatter(f_string);
    f.format("\nStore: %-20s Format: %-5s Capacity: %5dGB Unallocated: %5dGB",
             store.name(), 
             store.type(), 
             store.getTotalSpace()/gig, 
             store.getUnallocatedSpace()/gig);;
    

    您应该有如下内容:

    Formatter f = new Formatter(f_string);
    String f_string =  
        f.format("\nStore: %-20s Format: %-5s Capacity: %5dGB Unallocated: %5dGB",
                 store.name(), 
                 store.type(), 
                 store.getTotalSpace()/gig, 
                 store.getUnallocatedSpace()/gig);;
    
  2. # 2 楼答案

    ^{}不像您认为的那样工作,但实际上您并不需要使用它。改用String.format

            String f_string = String.format("\nStore: %-20s Format: %-5s Capacity: %5dGB Unallocated: %5dGB",
                    store.name(), 
                    store.type(), 
                    store.getTotalSpace()/gig, 
                    store.getUnallocatedSpace()/gig);