java文件輸入
① java文件輸入輸出
import java.util.*;
import java.io.*;
class Student {
private String name;
private int age;
private int javaScore;
private int cScore;
public Student(String name,int age) {
this.name = name;
this.age = age;
}
public void setJavaScore(int java) {
if(java < 101 && java >= 0) {
this.javaScore = java;
}else{
System.out.println("Wrong score !");
}
}
public int getJavaScore() {
return this.javaScore;
}
public void setCScore(int C) {
if(C < 101 && C >= 0) {
this.cScore = C;
}else{
System.out.println("Wrong score !");
}
}
public int getCScore() {
return this.cScore;
}
public String toString() {
return this.name+" "+this.age+" "+this.javaScore+" "+this.cScore+"\n";
}
}
class TestChengji {
public static void main(String [] args) throws Exception{
Student[] StudentInfo = new Student[3];
Scanner in = new Scanner(System.in);
System.out.println("請輸入學生姓名 年齡 c語言成績與java的成績,中間用空格隔開:");
System.out.println("例如: 小明 18 82 80");
for(int i=0;i<3;i++) {
System.out.println("請輸入數據:");
StudentInfo[i] = new Student(in.next(),in.nextInt());
StudentInfo[i].setCScore(in.nextInt());
StudentInfo[i].setJavaScore(in.nextInt());
}
in.close();
BufferedWriter writer = new BufferedWriter(new FileWriter("chengji.txt"));
for(int i=0;i<3;i++) {
writer.write(StudentInfo[i].toString());
writer.flush();
}
writer.close();
}
}
//試試
② 關於Java中向文件寫入數據的問題
可以使用java中的FileWriter類向文件中寫入數據。很簡單。代碼例子如下:
importjava.io.FileWriter;
importjava.io.IOException;
publicclassFilewriter{
privatestaticfinalStringLINE_SEPARATOR=System.getProperty("line.separator");
/**
*
*@paramargs
*@throwsIOException
*/
publicstaticvoidmain(String[]args)throwsIOException{
/**
*創建一個可以往文件中寫入字元數據的字元流輸出流對象
*創建時必須明確文件的目的地
*如果文件不存在,這回自動創建。如果文件存在,則會覆蓋。
*當路徑錯誤時會拋異常
*
*當在創建時加入true參數,回實現對文件的續寫。
*/
FileWriterfw=newFileWriter("C:\demo1.txt",false);
/**
*調用該對象的write方法,向文件寫入字元。
*
*其實寫入到了臨時存儲緩沖區中
*/
fw.write("hello world!");//windows中的換行為 unix下為 。
fw.write("hahaha");
/**
*進行刷新,將字元寫到目的地中。
*/
//fw.flush();
/**
*關閉流,關閉資源。在關閉前會調用flush方法刷新緩沖區。關閉後在寫的話,會拋IOException
*/
fw.close();
}
}
③ java輸入如何輸入到文件中並用界面顯示出來
//代碼很簡單我就不加註釋了。因為鍵盤輸入的話是採集不到空格,沒有加入Scanner
public class Test2 {
public static void main(String[] args) {
int a = 0, b = 0, c = 0,d=0;
int tem;
String str = "abcdABCD 123-0-=";
char[] ch = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
tem=ch[i];
if (tem == 32) {
a++;
} else if(tem>=65&&tem<=90||tem>=97&&tem<120){
b++;
} else if(tem>=48&&tem<=57){
c++;
}else{d++;}
}System.out.println("空格數:"+a);
System.out.println("字母數:"+b);
System.out.println("數字數:"+c);
System.out.println("其他字元"+d);
}
}
④ Java中如何實現文件的輸入輸出
程序如下:
<span style="color:#990000;">
</span>File file1 = new File("/home/a123/a");
if (file1.exists()) {
System.out.println("存在文件夾a");
} else {
file1.mkdir(); // 文件夾的創建 創建文件夾/home/a123/a
}
File file2 = new File("/home/a123/a/test");
if (file2.exists()) {
System.out.println("存在文件夾或者文件test");
} else {
try {
file2.createNewFile(); // 文件的創建,注意與文件夾創建的區別
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 最簡單的文件讀寫方法是使用類FileWriter
* (它的父類依次是java.io.OutputStreamWriter——>java.io.Writer——>java.lang.Object );
*/
// 下面是向文件file2裡面寫數據
try {
FileWriter fileWriter = new FileWriter(file2);
String s = new String("This is a test! \n" + "aaaa");
fileWriter.write(s);
fileWriter.close(); // 關閉數據流
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* 這樣寫數據的話,是完全更新文件test裡面的內容,即把以前的東西全部刪除,重新輸入。
* 如果不想刪除以前的數據,而是把新增加的內容增添在文件末尾處。只需要在創建FileWriter對象時候,使用另外一個構造函數即可:
* FileWriter fileWriter=new FileWriter(file2,true);
*/
// 下面是從文件file2讀東西
try {
FileReader fileReader = new FileReader(file2);
String s = null;
char ch;
try {
char[] c = new char[100];
fileReader.read(c,0,2); // 具體想得到文件裡面的什麼值(單個char?int?還是String?),
System.out.println(c);
fileReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* 具體想得到文件裡面的什麼值(單個char?int?還是String?),需要知道不通read的不同用法:
* 1. int read() 讀取單個字元。
* 2. int read(char[] cbuf) 將字元讀入數組。 可以再將字元型數組轉化位字元串
* 3. int read(char[] cbuf,int off,int len) 將字元讀入數組的某一部分。
* 這三個方法都返回一個int值,作用是:讀取的字元數,如果已到達流的末尾,則返回 -1.
*/
}
⑤ java如何使用文件位元組輸入流類 : FileInputStream類
文件位元組輸入流可以從指定路徑的文件中讀取位元組數據。文件位元組輸入流類繼承InputStream類,並實現了讀取輸入流的各種方法。
創建文件位元組輸入流創建的構造方法語法如下:
語法1:以File對象為參數創建FileInputStream實例
- new FileInputStream(File file)1
語法2:以文件對象的絕對路徑為參數創建FIleInputStream實例
- new FileInputStream(String filepath)
⑥ java中如何實現用鍵盤輸入內容到文件
step1:新建一個演示類demo
運行結果1:
please input 「學生姓名」
李小明
please input 「科目名稱」
數學
please input「科目成績」
98
學生個人信息
姓名:李小明
科目:數學
成績:98.0
等級:A+
運行結果2:
please input 「學生姓名」
王強
please input 「科目名稱」
英語
please input「科目成績」
52
學生個人信息
姓名:王強
科目:英語
成績:52.0
等級:D
⑦ Java中如何實現文件的輸入和輸出
程序如下:
<span style="color:#990000;">
</span>File file1 = new File("/home/a123/a");
if (file1.exists()) {
System.out.println("存在文件夾a");
} else {
file1.mkdir(); // 文件夾的創建 創建文件夾/home/a123/a
}
File file2 = new File("/home/a123/a/test");
if (file2.exists()) {
System.out.println("存在文件夾或者文件test");
} else {
try {
file2.createNewFile(); // 文件的創建,注意與文件夾創建的區別
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 最簡單的文件讀寫方法是使用類FileWriter
* (它的父類依次是java.io.OutputStreamWriter——>java.io.Writer——>java.lang.Object );
*/
// 下面是向文件file2裡面寫數據
try {
FileWriter fileWriter = new FileWriter(file2);
String s = new String("This is a test! \n" + "aaaa");
fileWriter.write(s);
fileWriter.close(); // 關閉數據流
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* 這樣寫數據的話,是完全更新文件test裡面的內容,即把以前的東西全部刪除,重新輸入。
* 如果不想刪除以前的數據,而是把新增加的內容增添在文件末尾處。只需要在創建FileWriter對象時候,使用另外一個構造函數即可:
* FileWriter fileWriter=new FileWriter(file2,true);
*/
// 下面是從文件file2讀東西
try {
FileReader fileReader = new FileReader(file2);
String s = null;
char ch;
try {
char[] c = new char[100];
fileReader.read(c,0,2); // 具體想得到文件裡面的什麼值(單個char?int?還是String?),
System.out.println(c);
fileReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* 具體想得到文件裡面的什麼值(單個char?int?還是String?),需要知道不通read的不同用法:
* 1. int read() 讀取單個字元。
* 2. int read(char[] cbuf) 將字元讀入數組。 可以再將字元型數組轉化位字元串
* 3. int read(char[] cbuf,int off,int len) 將字元讀入數組的某一部分。
* 這三個方法都返回一個int值,作用是:讀取的字元數,如果已到達流的末尾,則返回 -1.
*/
}
⑧ java實現文件的輸入輸出的重要性是什麼
輸入和輸出功能是Java對程序處理數據能力的提高,Java以流的形式處理數據。流是一組有序的數據序列,根據操作的類型,分為輸入流和輸出流。
程序從輸入流讀取數據,向輸出流寫入數據。Java是面向對象的程序語言,每一個數據流都是一個對象,它們提供了各種支持「讀入」與「寫入」操作的流類。
⑨ java中如何做文件位元組輸入流
首先,我以往經驗是寫入位元組流和輸出位元組流的方式一定要一致,不然寫入的位元組流就會出錯甚至讀取不到,下面是位元組流的寫入和讀取方法:
importjava.io.*;
classTest{
publicstaticvoidmain(Stringargs[]){
FileInputStreamfis=null;
FileOutputStreamfos=null;
byte[]buffer=newbyte[100];
inttemp=0;
try{
fis=newFileInputStream("D:/wenhao/src/from.txt");
fos=newFileOutputStream("D:/wenhao/src/to.txt");
while(true){
temp=fis.read(buffer,0,buffer.length);
if(temp==-1){
break;
}
fos.write(buffer,0,temp);
}
}
catch(Exceptione){
System.out.println(e);
}
finally{
try{
fis.close();
fos.close();
}
catch(Exceptione2){
System.out.println(e2);
}
}
}
}