Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Printwriter未将前导零输出到文本文件_Java - Fatal编程技术网

Java Printwriter未将前导零输出到文本文件

Java Printwriter未将前导零输出到文本文件,java,Java,这段代码我试图以某种格式输出到文本文件,这样当我加载它时就不会有任何问题。我正在使用String.format(“%02d”,5)来输出05,而不仅仅是5。这在控制台中工作,但当输出到文本文件时,它似乎输出为5而不是05。为什么会这样 PrintWriter output = new PrintWriter(scheduleFile); for (int i = 0; i < getSchedule().length; i++) { for (int j = 0; j <

这段代码我试图以某种格式输出到文本文件,这样当我加载它时就不会有任何问题。我正在使用
String.format(“%02d”,5)
来输出
05
,而不仅仅是
5
。这在控制台中工作,但当输出到文本文件时,它似乎输出为5而不是05。为什么会这样

PrintWriter output = new PrintWriter(scheduleFile);

for (int i = 0; i < getSchedule().length; i++) {
    for (int j = 0; j < getSchedule()[i].length; j++) {
        output.print(j + ":00-" + (j + 1) + ":00     =");
        if (getSchedule()[i][j] != null) { // if not null
            output.print(String.format("%02d", (j)) + ":00-" + String.format("%02d", (j+1)) + ":00     "); // print hours
        }
        output.println();
    }
}
应该是这样的:

0:00-1:00     =  
1:00-2:00     =  
2:00-3:00     =  
3:00-4:00     =  
4:00-5:00     =  
5:00-6:00     =  
6:00-7:00     =  
7:00-8:00     =  
8:00-9:00     =  
9:00-10:00     =  
.  
.  
.  
00:00-01:00     =  
01:00-02:00     =  
02:00-03:00     =  
03:00-04:00     =  
04:00-05:00     =  
05:00-06:00     =  
06:00-07:00     =  
07:00-08:00     =  
08:00-09:00     =  
09:00-10:00     =   
.  
.  
.  

这意味着此行将打印到您的文件
output.print(j+“:00-”+(j+1)+“:00=”)
,而不是这个
output.print(String.format(“%02d”,(j))+“:00-“+String.format(“%02d”,(j+1))+”:00”)。只需将
String.format
添加到第一个
output.print
行。这意味着这一行将打印到您的文件
output.print(j+“:00-”+(j+1)+“:00=”)
,而不是这个
output.print(String.format(“%02d”,(j))+“:00-“+String.format(“%02d”,(j+1))+”:00”)。只需将
String.format
添加到第一个
output.print
行。