Java 1.5 Tiger A Developers Notebook [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Java 1.5 Tiger A Developers Notebook [Electronic resources] - نسخه متنی

David Flanagan, Brett McLaughlin

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید








9.4 Using the printf( ) Convenience Method


For those of you who are die-hard

C and C++ fans, Tiger gives you the
ability to type printf( ) once more.


9.4.1 How do I do that?


In "Using the format( ) Convenience Method," you saw how both the
PrintStream and PrintWriter classes offer a new method called
format( ) to handle formatted output. Each class also has a method
called printf( ), which does exactly the same thing. That's right
printf( ) and format( ) are interchangeable. So, if you favor using
printf( ) over format( ), you're free to do so, as Example 9-1 shows.


Example 9-1. Using printf( ) instead of format( )


package com.oreilly.tiger.ch09;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class FormatTester {
public static void main(String[] args) {
String filename = args[0];
try {
File file = new File(filename);
FileReader fileReader = new FileReader(file);
BufferedReader reader = new BufferedReader(fileReader);
String line;
int i = 1;
while ((line = reader.readLine( )) != null) {
System.out.printf("Line %d: %s%n", i++, line);
}
} catch (Exception e) {
System.err.printf("Unable to open file named '%s': %s",
filename, e.getMessage( ));
}
}
}


/ 131