Java_Basic_Day08-字符串+IO+方法参数

1.字符串

1.1 比较

equals(Object obj)

equalsIgnoreCase(String str)

1.2 替换

replace(CharSequence oldStr,CharSequence newStr)

1.3切割

split(String regex)

英文“.”使用“\\.”

public static void main(String[] args) {
        String a = "192.168.128.33";
        String b = "小米1x";
        //比较
        System.out.println(a.equals(b));
        System.out.println(a.equalsIgnoreCase(b));
        //替换
        String a1 = a.replace("1","2");
        System.out.println(a1);
        //切割
        String[] a2 = a.split("\\.");
        String a3 = "";
        for (int i = 0; i < a2.length; i++){
            System.out.println(a2[i]);
            a3 += a2[i];
        }
        System.out.println(a3);

    }

2.IO流

2.1fielWriter

public static void main(String[] args) throws IOException {
        //IO写入文件


                //创建对象
                FileWriter fw = new FileWriter("C:\\cz_java\\ps_java_basic\\src\\day08\\test.txt",true);
                //写入数据
                //换行符
                fw.write("\r\n");
                fw.write("我们都是好孩子");
                //unicode
                fw.write(97);
                //写string一部分
                String str = "去年那年的冬天";
                fw.write(str,3,4);
                //完整字符数组
                char[] array = {'1','g','h','3','d'};
                fw.write(array,3,2);
                fw.write("\r\n");
                fw.write(array);
                //关闭
                fw.close();



    }

2.2 fileReader

public static void main(String[] args) throws IOException {
        //创建文件流
        try (FileReader fr = new FileReader("C:\\cz_java\\ps_java_basic\\src\\day08\\test.txt")) {
            //读取
            int ch;
            while (( ch = fr.read()) != -1){
                System.out.print((char) ch);
            }
            //关闭
            fr.close();
        }
    }

2.3buf

public static void main(String[] args) throws IOException{
        //读取字符数组
        FileReader fr = new FileReader("C:\\cz_java\\ps_java_basic\\src\\day08\\test.txt");
        char[] buf = new char[2];
        int len; //代表有效长度
        while ((len = fr.read(buf)) != -1){
            //读取
            String str = new String(buf,0,len);
            System.out.print(str);
        }

        //关闭
        fr.close();
    }

2.4BufferedWriter

构造方法

public BufferedWriter(FileWriter fw) :参数就是一个普通的FileWriter对象;
package day08;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class TestBufferedWriter {
    public static void main(String[] args) throws IOException{
        //创建FileWriter
        FileWriter fw = new FileWriter("C:\\cz_java\\ps_java_basic\\src\\day08\\test.txt",true);
        //创建buff
        BufferedWriter fb = new BufferedWriter(fw);
        //写入
        fb.newLine();
        fb.write("今天下午学习");
        //关闭
        fb.close();
    }
}

2.5bufferedReader

package day08;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TestBufferedReader {
    public static void main(String[] args) throws IOException{
        //创建读取
        FileReader fr = new FileReader("C:\\cz_java\\ps_java_basic\\src\\day08\\test.txt");
        //创建buff
        BufferedReader br = new BufferedReader(fr);
        //循环读取
       /* int ch;
        while((ch = br.read()) != -1){
            System.out.print((char)ch);
        }*/

        //读取字符数组
        char[] buf = new char[2];//一次取两个
        int len = br.read(buf);//代表有效个数
        
        while ((len = br.read(buf)) != -1){
            String str = new String(buf,0,len);
            System.out.println(str);
        }

        //关闭
        br.close();
    }
}
readLine();
package day08;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TestBufferedReader {
    public static void main(String[] args) throws IOException{
        //创建读取
        FileReader fr = new FileReader("C:\\cz_java\\ps_java_basic\\src\\day08\\test.txt");
        //创建buff
        BufferedReader br = new BufferedReader(fr);
        //循环读取
       /* int ch;
        while((ch = br.read()) != -1){
            System.out.print((char)ch);
        }*/

        //读取字符数组
        /*char[] buf = new char[2];//一次取两个
        int len = br.read(buf);//代表有效个数

        while ((len = br.read(buf)) != -1){
            String str = new String(buf,0,len);
            System.out.println(str);
        }*/

        //读取一行
        String s1 = br.readLine();
        System.out.println(s1);

        //关闭
        br.close();
    }
}

循环读取

package day08;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TestBufferedReader {
    public static void main(String[] args) throws IOException{
        //创建读取
        FileReader fr = new FileReader("C:\\cz_java\\ps_java_basic\\src\\day08\\test.txt");
        //创建buff
        BufferedReader br = new BufferedReader(fr);
        //循环读取
       /* int ch;
        while((ch = br.read()) != -1){
            System.out.print((char)ch);
        }*/

        //读取字符数组
        /*char[] buf = new char[2];//一次取两个
        int len = br.read(buf);//代表有效个数

        while ((len = br.read(buf)) != -1){
            String str = new String(buf,0,len);
            System.out.println(str);
        }*/

        //读取一行
        /*String s1 = br.readLine();
        System.out.println(s1);*/

        //读取很多
        String line;
        while ((line = br.readLine())!= null){
            System.out.println(line);
        }

        //关闭
        br.close();
    }
}

练习1:

//定义一个集合用于存储多个字符串,向其中添加一些字符串;
//然后将集合的所有字符串内容写到文件中,要求每个字符占一行;

package day08;
//定义一个集合用于存储多个字符串,向其中添加一些字符串;
//然后将集合的所有字符串内容写到文件中,要求每个字符占一行;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class FromListToFile {
    public static void main(String[] args) throws IOException{
        //定义集合
        ArrayList<String> list = new ArrayList<>();
        //添加字符串
        list.add("迪丽热巴");
        list.add("赵丽颖");
        list.add("何一");


        //写入文件
        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\cz_java\\ps_java_basic\\src\\day08\\test.txt",true));
        for (int i = 0; i < list.size(); i++){
            String str = list.get(i);//当前字符串
            bw.write(str);
            bw.newLine();//换行
        }

        //关闭
        bw.close();
    }
}

练习2:

//将文件中的字符串文本读取到集合中,并且每一行文本作为集合中一个字符串元素

package day08;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

//将文件中的字符串文本读取到集合中,并且每一行文本作为集合中一个字符串元素
public class FileToList {
    public static void main(String[] args) throws IOException{
        //创建集合
        ArrayList<String> list = new ArrayList<>();
        //读取文件
        BufferedReader br = new BufferedReader(new FileReader( "C:\\cz_java\\ps_java_basic\\src\\day08\\test.txt"));
        String line;
        while((line = br.readLine())!= null){
            //循环放到集合
            list.add(line);
        }
        //关闭
        br.close();

        //遍历集合
        for (int i = 0; i < list.size(); i++){
            System.out.println(list.get(i));
        }
    }
}

练习3:反转一个文件中字符串

原文件
世界上怎么会有你这种人
一点也不知道温柔体贴
生气的时候凶的像条狗
一天到晚就知道瞎BB
你以为你是谁啊
爱咋咋地
我特么就这样

反转后

我爱你一生一世

package day08;

import java.io.*;
import java.util.ArrayList;

public class PoemReverse {
    public static void main(String[] args) throws IOException{
        //读取文件 BufferedReader
        BufferedReader br = new BufferedReader(new FileReader("C:\\cz_java\\ps_java_basic\\src\\day08\\test.txt"));
        //定义一个集合 ArrayList<String>
        ArrayList<String> list = new ArrayList<>();
        //添加add,读取一行readLine
        String line;
        while ((line = br.readLine()) != null){
            list.add(line);
        }
        br.close();
        //写入另一个文件
        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\cz_java\\ps_java_basic\\src\\day08\\result.txt"));

        //倒序遍历
        for(int i = list.size()-1; i >= 0 ; i--) {

            bw.write(list.get(i),0,1);
            //bw.newLine();
        }
        //关闭

        bw.close();

    }
}

3.方法参数

所有数据类型都可以作为方法的参数类型;

基本类型

数组

字符串

自定义的类

package day08;

public class Testboy {
    public static void main(String[] args) {
        //方法参数为对象
        Method(new Boy("何一",23));
    }

    public static void Method(Boy boy){
        System.out.println(boy.getName()+" "+boy.getAge());
    }
}

4.方法返回值

所有数据类型都可以作为方法的返回值类型;

基本类型

数组

字符串

自定义的类

package day08;

public class MethodReturn {
    public static void main(String[] args) {
        //对象作为返回值类型
        Boy nan = method2();
        System.out.println(nan.getName()+" "+nan.getAge());
    }

    public static Boy method2(){
        Boy nansheng = new Boy("李连杰",50);
        return nansheng;

    }
}
5.静态方法和成员方法区别


调用静态方法格式:

    方法名(参数值); //直接调用本类当中的静态方法

    类名称.方法名(参数值);  //调用另外一个类中的静态方法

调用成员方法格式:

    类名称.对象名 = new 类名称(); //首先创建对象

    对象名.成员方法名(参数值); //通过对象名调用成员方法

package day08;

public class StaticVsInstance {
    public static void main(String[] args) {
        //本类中
        method();
        //其他类中
        OtherMethod.method2();

        //成员方法
        StaticVsInstance si = new StaticVsInstance();
        si.method3();

        //另外一个类中的成员方法
        OtherMethod om = new OtherMethod();
        om.method4();

    }

    public static void method(){
        System.out.println("静态1");
    }
    public void method3(){
        System.out.println("成员方法1");
    }
}
package day08;

public class OtherMethod {
    public static void method2(){
        System.out.println("静态2");
    }

    public void method4(){
        System.out.println("成员方法2");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值