Scanner input = new Scanner(new File("temp.txt"));
System.out.print(input.nextLine());
package cn.netkiller.io;
import java.io.File;
import java.io.FileNotFoundException;
//import java.io.PrintWriter;
import java.util.Scanner;
public class PrintWriterTest {
private static Scanner input;
public PrintWriterTest() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
// PrintWriter output = new PrintWriter("temp.txt");
// output.print("Welcome to Java!");
// output.close();
input = new Scanner(new File("temp.txt"));
System.out.print(input.nextLine());
}
}
@Test
void testReadFile1() throws IOException {
String fileName = "D:\\test\\netkiller\\neo.txt";
try (Scanner sc = new Scanner(new FileReader(fileName))) {
while (sc.hasNextLine()) { //按行读取字符串
String line = sc.nextLine();
System.out.println(line);
}
}
try (Scanner sc = new Scanner(new FileReader(fileName))) {
sc.useDelimiter("\\|"); //分隔符
while (sc.hasNext()) { //按分隔符读取字符串
String str = sc.next();
System.out.println(str);
}
}
//sc.hasNextInt() 、hasNextFloat() 、基础数据类型等等等等。
fileName = "D:\\netkiller\\test\\neo.txt";
try (Scanner sc = new Scanner(new FileReader(fileName))) {
sc.useDelimiter("\\|"); //分隔符
while (sc.hasNextInt()) { //按分隔符读取Int
int intValue = sc.nextInt();
System.out.println(intValue);
}
}
}