1. 程式人生 > >4.26 string 方法 file建立 inputstream和outputstream

4.26 string 方法 file建立 inputstream和outputstream

一、string 方法

方法名    說明
public int indexOf(int ch)搜尋第一個出現的字元ch,如果沒有找到,返回-1
public int indexOf(string value )搜尋第一個出現的字串value,如果沒有找到,返回-1
public int lastIndexOf(int  ch)搜尋最後一個出現的字元ch,如果沒有找到,返回-1
public int lastIndexOf(string value)搜尋最後一個出現的字串value,如果沒有找到,返回-1
public substring(int  index提取從位置索引開始的字串部分
public substring(int begin
index,int endindex
提取從 beginindex到endindex的字串部分
public string trim()返回前後不含空格的呼叫字串副本

註釋:1、擷取的時候包含起始位置,不包含末尾位置。

          2、擷取時,第一個字元下標是從0開始的。length從1開始算起;

public static void main(String[] args) throws IOException {


String s="我愛你,祖國!";
s.indexOf("你");

System.out.println(s.indexOf("!"));
System.out.println(s.length());
System.out.println(s.lastIndexOf("祖"));
System.out.println(s.substring(2));
System.out.println(s.substring(1, 5));

}

二、file建立

1、檔案:相關記錄或存放在一起的資料集合。

    java程式如何訪問檔案屬性:JAVA API:java.io.File類

    a、建立檔案:

        File file = new File(檔案路徑 // c:\\texy.txt或者c:/texy.txt)

2、file類的常用方法

方法名說明
boolean exists()判斷檔案或目錄是否存在
boolean isFile判斷是否是檔案
boolean isDiretory判斷是否是目錄
String getPath()返回此物件表示的檔案的相對路徑名
String getAbsolutePath()返回此物件表示的檔案的絕對路徑名
String getName返回此物件表示的檔案或目錄的名稱
boolean delete()刪除此物件指定的檔案或目錄、
boolean createNewFile()建立名稱的空檔案,不建立資料夾
long length()返回檔案的長度、單位為位元組,如果檔案不存在則返回ol
註釋:檔案需要捕捉異常

File file=new File ("E:\\水調歌頭.txt");


if(!file.exists()){
file.createNewFile();
System.out.println(file.getName());
System.out.println(file.getAbsolutePath());
System.out.println(file.getPath());
System.out.println(file.delete());

}

三、流

流:1、流是一組有序的資料序列。2、以先進先出的方式傳送資訊的通道。

我們是站在程式的角度讀入(input )和讀出(output)

1、流的分類

按流向分:輸出流(outputStream和Writer作為基類)和輸入流(inputStream和Reader作為基類)

    輸入輸出流是相對於計算機記憶體來說的。

按處理資料單元劃分:位元組流(outputStream和inputStream)和字元流(Writer和Reader)

2、InputStream常用方法(抽象類父類)

        int read()   從輸入流一個位元組一個位元組的讀,返回的是該位元組的整數表示形式,如果輸入到流的末尾返回-1

        int read(byte[] b)  從輸入流讀取若干位元組,把這些位元組儲存到陣列b中 ,返回的是位元組數,末尾返回-1

        int read(byte[] b,int off,int len)  讀取從開始儲存資料的起始下標off到l想讀取的位元組數目len到陣列b中

        void close;關閉輸入流

        int available():可以從輸入流中讀取位元組數目;

FileInputStream作為InputStream的子類;構造方法

FileInputStream(File file)

FileInputStream(String name)

3、使用FileInputStream讀取檔案

引入相關類——>構造檔案流FileInputStream物件——>讀取檔案資料——>關閉檔案流物件

InputStream file=null;
try {
file = new FileInputStream("e:\\水調歌頭.txt");
System.out.println(file.available());
int data;
while (( data=file.read())!=-1) {
System.out.println(data);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
file.close();
}

4、FileOutputStream方法同InputStream

        FileOutputStream(string name,boolean append)

5、將檔案裡的內容讀取到另外一個檔案

//public static void main(String[] args) throws IOException {

//File file=new File("E:\\轉移.txt");
//if (!file.exists()) {
//file.createNewFile();
//}
//InputStream e=null;
//OutputStream f=null;
//
//e=new FileInputStream("e:\\水調歌頭.txt");
//f=new FileOutputStream("e:\\轉移.txt",false);
//
//int data=e.read();
//while ((data=e.read())!=-1) {
//f.write((char)data);
//}
//e.close();
//f.close();

//               }