java Print的使用


1、Print流的使用

package cn.kongxh.io7;

import java.io.* ;
public class PrintDemo01{
    public static void main(String arg[]) throws Exception{
        PrintStream ps = null ;        // 聲明打印流對象
        // 如果現在是使用FileOuputStream實例化,意味着所有的輸出是向文件之中
        ps = new PrintStream(new FileOutputStream(new File("d:" + File.separator + "test.txt"))) ;
        ps.print("hello ") ;
        ps.println("world!!!") ;
        ps.print("1 + 1 = " + 2) ;
        ps.close() ;
    }
};

2、

 1 package cn.kongxh.io7;
 2 
 3 import java.io.* ;
 4 public class PrintDemo02{
 5     public static void main(String arg[]) throws Exception{
 6         PrintStream ps = null ;        // 聲明打印流對象
 7         // 如果現在是使用FileOuputStream實例化,意味着所有的輸出是向文件之中
 8         ps = new PrintStream(new FileOutputStream(new File("d:" + File.separator + "test.txt"))) ;
 9         String name = "李興華" ;    // 定義字符串
10         int age = 30 ;                // 定義整數
11         float score = 990.356f ;    // 定義小數
12         char sex = 'M' ;            // 定義字符
13         ps.printf("姓名:%s;年齡:%d;成績:%f;性別:%c",name,age,score,sex) ;
14         ps.close() ;
15     }
16 };

3、

 1 package cn.kongxh.io7;
 2 
 3 import java.io.* ;
 4 public class PrintDemo03{
 5     public static void main(String arg[]) throws Exception{
 6         PrintStream ps = null ;        // 聲明打印流對象
 7         // 如果現在是使用FileOuputStream實例化,意味着所有的輸出是向文件之中
 8         ps = new PrintStream(new FileOutputStream(new File("d:" + File.separator + "test.txt"))) ;
 9         String name = "李興華" ;    // 定義字符串
10         int age = 30 ;                // 定義整數
11         float score = 990.356f ;    // 定義小數
12         char sex = 'M' ;            // 定義字符
13         ps.printf("姓名:%s;年齡:%s;成績:%s;性別:%s",name,age,score,sex) ;
14         ps.close() ;
15     }
16 };

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM