Java/File Input Output/Scanner — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 06:04, 1 июня 2010
Содержание
- 1 new Scanner(new BufferedReader(new FileReader("xanadu.txt")))
- 2 Perform the conversions after parsing the String
- 3 Read file using Scanner class
- 4 Read in the current contents of the java.net homepage
- 5 Read user input from console using Scanner class
- 6 Scanning text with java.util.Scanner
- 7 Simplify the code in TextReader by using java.util.Scanner
- 8 Split a string using Scanner class
new Scanner(new BufferedReader(new FileReader("xanadu.txt")))
/*
* Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Sun Microsystems nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class ScanXan {
public static void main(String[] args) throws IOException {
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));
while (s.hasNext()) {
System.out.println(s.next());
}
} finally {
if (s != null) {
s.close();
}
}
}
}
Perform the conversions after parsing the String
import java.io.File;
import java.util.Scanner;
public class Main {
static void parseLine(String line) {
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter("\\s*,\\s*");
String name = lineScanner.next();
int age = lineScanner.nextInt();
boolean isCertified = lineScanner.nextBoolean();
System.out.println("It is " + isCertified + " that " + name + ", age " + age
+ ", is certified.");
}
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(new File("fileName"));
scanner.useDelimiter(System.getProperty("line.separator"));
while (scanner.hasNext()) {
parseLine(scanner.next());
}
scanner.close();
}
}
Read file using Scanner class
import java.io.File;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception{
File file = new File("data.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
}
}
Read in the current contents of the java.net homepage
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
URLConnection connection = new URL("http://java.net").openConnection();
String text = new Scanner(connection.getInputStream()).useDelimiter("\\Z").next();
}
}
Read user input from console using Scanner class
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Username: ");
String username = scanner.nextLine();
System.out.print("Password: ");
String password = scanner.nextLine();
System.out.print("What is 2 + 2: ");
int result = scanner.nextInt();
if (username.equals("admin") && password.equals("secret") && result == 4) {
System.out.println("Welcome");
} else {
System.out.println("Invalid username or password!");
}
}
}
Scanning text with java.util.Scanner
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Main {
private static void readFile(String fileName) throws Exception {
File file = new File(fileName);
FileReader reader = new FileReader(file);
BufferedReader in = new BufferedReader(reader);
String string;
while ((string = in.readLine()) != null) {
System.out.println(string);
}
in.close();
}
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("usage: java TextReader " + "file location");
System.exit(0);
}
readFile(args[0]);
}
}
Simplify the code in TextReader by using java.util.Scanner
import java.io.File;
import java.util.Scanner;
public class Main {
static void readFile(String fileName) throws Exception {
File file = new File(fileName);
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
System.out.println(scanner.next());
}
scanner.close();
}
public static void main(String[] args) throws Exception{
if (args.length != 1) {
System.err.println("usage: java TextScanner1" + "file location");
System.exit(0);
}
readFile(args[0]);
}
}
Split a string using Scanner class
import java.io.File;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
File file = new File("data.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter(",");
while (lineScanner.hasNext()) {
String part = lineScanner.next();
System.out.print(part + ", ");
}
System.out.println();
}
}
}