首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java:将字符串写入.txt问题

Java:将字符串写入.txt问题
EN

Stack Overflow用户
提问于 2015-08-16 18:58:35
回答 2查看 45关注 0票数 0

以下是我的程序的较长但已注释的代码:

代码语言:javascript
复制
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();
    }
}

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

任何建议都将不胜感激。

附注:我曾经考虑过使用Writer,但为了了解我自己的知识,我正在使用channel/buffer对象。但是,如果你知道一些特殊的原因,为什么作家会更好,请让我知道:)。

EN

回答 2

Stack Overflow用户

发布于 2015-08-16 19:05:45

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

而不是

代码语言:javascript
复制
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);;

你应该有类似这样的东西:

代码语言:javascript
复制
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);;
票数 0
EN

Stack Overflow用户

发布于 2015-08-16 19:07:45

Formatter并不像您想象的那样工作,但您实际上并不需要使用它。请改用String.format

代码语言:javascript
复制
        String f_string = String.format("\nStore: %-20s Format: %-5s Capacity: %5dGB Unallocated: %5dGB",
                store.name(), 
                store.type(), 
                store.getTotalSpace()/gig, 
                store.getUnallocatedSpace()/gig);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32034287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档