Java中String类和IO流——学习小结

1.String类

知识点:
1.String认识
2.比较方式
3.判断方法
4.转换方法
5.其他一些方法

1.1String类认识

String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。String类对象创建后是一个常量,有final修饰,不能被修改。字符串重新赋值不是修改,而是生成了一个新的字符串,原先的字符串依旧在内存中,但是引用变量不再指向它,过后原字符串因没被使用也会被Java GC机制回收。

1.2String类的比较方式

比较方式有两种

	==		比较两个变量的地址
	equals	比较两个变量包含的内容
String str1 = "abc";	//创建了一个对象在字符串池中存储
String str2 = "abc";	//从字符串池中引用了"abc",地址与a相同
String str3 = new String("abc");	//在堆中创建新对象,不复用池中的"abc"。此时有两个"abc",一个在堆中,一个在池中
//用 == 比较:
System.out.printl(str1 == str2);	//true
System.out.println(str1 == str3);	//false
System.out.println(str2 == str3);	//false
//用equals方法比较:
System.out.println(str1.equals(str2));	//true
System.out.println(str1.equals(str3));	//true
System.out.println(str2.equals(str3));	//true

因此要求比较字符串用equals方法。

1.3String类的获取方法

(1)以下方法都是找出指定元素第一次出现的下标位置(可限制开始查找的位置)

  • int length(); 获取字符串长度
  • char charAt(int index); 获取String字符串中指定下标位置的char类型字符,如果index超出有效范围,出现异常:StringIndexOutOfBoundsException(字符串下标越界异常)
  • int indexOf(char ch);获取单字符第一次出现下标
  • int indexOf(String str);获取字符串第一次出现下标
  • int indexOf(char ch, int fromIndex);从指定下标位置开始,查询指定字符第一次出现的下标
  • int indexOf(String str, int fromIndex); 同上

(2)以下方法都是找出指定元素最后一次出现的下标位置(可限制开始查找的位置)

  • int lastIndexOf(char ch);
  • int lastIndexOf(char ch);
  • int lastIndexOf(char ch, int fromIndex);
  • int lastIndexOf(String str, int fromIndex);

1.4String类判断方法

  • boolean endsWith(String str);
    ——判断当前字符串是不是以指定字符串结尾
  • boolean isEmpty();
    ——判断字符串是否为空 ""空串 JDK1.6之后 null不能读取,不能写入,不能调用方法
  • boolean equals(Object obj);
    ——继承重写Object类内的方法,完成字符串要求的比较方式
  • boolean equalsIgnoreCase(String str);
    ——不区分大小写比较
  • boolean contains(String str);
    ——判断指定字符串是否存在

1.5String转换方法

  • String(char[] arr);
    ——使用字符数组中内容创建一个字符串对象

  • String(char[] arr, int offset, int length);
    ——使用字符数组中内容创建一个字符串对象,offset是从char类型数组中指定下标位置开始获取数据,获取的数据长度是length

  • static String valueOf(char[] arr);
    ——通过类名调用的静态方法,实际执行的是String(char[] arr);

  • static String valueOf(char[] arr, int offset, int length);
    ——通过类名调用的静态方法,实际执行的是String(char[] arr, int offset, int length);

  • char[] toCharArray();
    ——返回当前字符串对应的字符数组

1.6其他方法

  • String replace(char oldChar, char newChar)
    ——替换,替换不会修改原始的字符串,而是创建一个新字符串返回,并且替换效果是所有的对应 的oldChar全部替换成newChar
  • String[] split(String regex)
    ——按照指定的字符串切割当前字符串
  • String substring(int beginIndex)
    ——从指定位置开始,截取子字符串,到字符串末尾
  • String substring(int beginIndex, int endIndex)
    ——从指定位置开始beginIndex,到endIndex结束,要头不要尾
  • String toUpperCase()
    ——转大写字符串小写转大写
  • String toLowerCase()
    ——转小写字符串大写转小写
  • String trim()
    ——去除空格去除字符串两边的无用空格

2.IO流

知识点:
1.什么是Io流?
2.IO流分类
3.文件操作字节流
4.文件操作字符流

2.1什么是IO流?

  • I —— input 输入流
  • O —— output 输出流
    输入输出走向,是按照当前程序使用的内存为参照物来考虑的
    比如:
    从内存中把数据保存到硬盘中—— output
    从硬盘中读取数据到内存中—— input

IO流基类:

  • InputStream 输入流基类 read
  • OutputStream 输出流基类 write

此时注意:
【完整的文件】拷贝效率要远远大于相同大小的【散文件】的拷贝效率

2.2 IO流分类

(1)按流向:

  • 输入
  • 输出

(2)按操作处理单元:

  • 字节流
    FileInputStream —— 文件操作输入字节流
    FileOutputStream —— 文件操作输出字节流
  • 字符流
    FileReader —— 文件操作输入字符流
    FileWriter —— 文件操作输出字符流

2.3文件操作字节流

2.3.1文件操作输入字节流

FileInputStream
(1)构造方法

  • FileInputStream(File file); 这里是根据提供的File类对象创建对应的文件操作输入字节流。
  • FileInputStream(String pathName); 这里是根据提供的String类型文件路径,创建对应的文件操作输入字节流。

以上两个方法都会抛出异常:
FileNotFoundException 文件未找到异常。
(2)成员方法

  • int read(); 从文件中读取一个字节数据返回到方法外。虽然返回值是一个int类型,但是在整个int类型当中存储的数据是一个byte类型,有且只有低8位数据有效
  • nt read(byte[] buf); 读取文件的内容是存储在byte类型数组中,返回值是读取到的字节个数
  • int read(byte[] buf, int offset, int count);读取文件的内容是存储在byte类型数组中,要求从byte数组offset位置开始,到count长度结束,返回值是读取到的字节个数

以上三个方法如果读取到文件末尾,返回值都是 -1 EOF (End Of File)。而且以上方法都要异常抛出IOException IO异常

FileInputStream代码演示:

// 		1. 确定操作文件
		File file = new File("C:\\aaa\\1.txt");
		
		// 2. 字节输入流读取文件对象
		FileInputStream fileInputStream = null;
		
		try {
			// 3. 根据File类对象创建对应的字节输入流
			fileInputStream = new FileInputStream(file);
			
			// 4. 准备一个8KB字节缓冲数组
			byte[] buf = new byte[1024 * 8];
			int length = -1;
			
			// 5. 读取数据
			while ((length = fileInputStream.read(buf)) != -1) {
				System.out.println(new String(buf, 0, length));
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 在finally代码块中关闭资源
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
2.3.2文件操作输出字节流

FileOutputStream
(1)构造方法

  • FileOutputStream(File file);根据File类对象创建对应的文件输出字节流对象
  • FileOutputStream(String pathName);根据String类型文件路径创建对应的文件输出字节流对象

以上两个构造方法,创建的FileOutputStream是删除写/清空写操作,会将原文件中的内容全部删除之后,写入数据。

  • FileOutputStream(File file, boolean append);根据File类对象创建对应的文件输出字节流对象。创建对象时给予append参数为true,表示追加写。
  • FileOutputStream(String pathName, boolean append);根据String类型文件路径创建对应的文件输出字节流对象,创建对象时给予append参数为true,表示追加写。

FileOutputStream构造方法是拥有创建文件的内容,如果文件存在,不创建,文件不存在且路径正确,创建对应文件。否则抛出异常FileNotFoundException
(2)成员方法

  • void write(int b);写入一个字节数据到当前文件中,参数是int类型,但是有且只会操作对应的低八位数据
  • void write(byte[] buf);写入字节数组中的内容到文件中
  • void write(byte[] buf, int offset, int length);写入字节数组中的内容到文件中,从指定的offset开始,到指定长度length

以上方法会抛出异常:IOException

FileOutputStream代码演示:

		// 1. 确定文件
		File file = new File("C:/aaa/中国加油.txt");
		
		// 2. 文件操作字节输出流对象
		FileOutputStream fileOutputStream = null;
		
		try {
			// 3. 创建FileOutputStream 
			fileOutputStream = new FileOutputStream(file);
			
			// 4. 准备byte类型数组
			byte[] bytes = "武汉加油!中国加油!".getBytes();
			byte[] arr = {65, 66, 67, 68, 69, 70, 71};
			
			fileOutputStream.write(bytes);
			fileOutputStream.write(arr, 2, 3);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭资源
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

2.4文件操作字符流

2.4.1字符流特征

字符流 = 字节流 + 解码过程
字节组合操作 ==> 对应当前环境编码集的一个字符
如果字符找不到,该数据无效,需要被删除。

2.4.2文件操作输入输出字符流

同字节流

发布了6 篇原创文章 · 获赞 2 · 访问量 598

猜你喜欢

转载自blog.csdn.net/qq_41986648/article/details/104521391