//从现有文件中读取文件中的内容到控制台输出
import java.io.FileInputStream;
import java.io.IOException;
public class getFileContent {
public static void main(String[] args)throws IOException{FileInputStream fis = new FileInputStream("file.txt");
byte[] bbuf = new byte[1024];
int hasRead = 0 ;
while((hasRead = fis.read(bbuf)) > 0){
System.out.print(new String(bbuf,0,hasRead));
}
fis.close();
}
}
//从现有文件中读取文件中内容到新文件
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class getFileContent{
public static void main(String[] args)throws IOException{
FileInputStream fis = new FileInputStream("file.txt");
FileOutputStream fos = new FileOutputStream("newFile.txt");
byte[] bbuf = new byte[1024];
int hasRead = 0;
while((hasRead = fis.read(bbuf)) > 0){
fos.write(bbuf,0,hasRead);
}
fis.close();
}
}
//从键盘输入,在控制台输出
import java.util.Scanner;
public class OutFileContent{
public static void main(String[] args){Scanner sc = new Scanner(System.in);
sc.useDelimiter("\n");
while(sc.hasNext()){
System.out.println("键盘输入的内容为:" + sc.next());
}
}
}
//从键盘输入,在指定文件中输出
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;
import java.io.FileOutputStream;
public class OutFileContent{public static void main(String[] args)throws IOException{
Scanner sc = new Scanner(System.in);
PrintStream ps = new PrintStream(new FileOutputStream("out.txt"));
System.setOut(ps);
sc.useDelimiter("\n");
while(sc.hasNext()){
System.out.println("键盘输入的内容为:" + sc.next());
}
}
}