Java-StringBuffer和StringBuilder

package com.example.demo.StringTest;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.*;

/**
 * @author hanqiang
 * @Classname StringTest
 * @Date 2021/5/20 14:23
 * @Description:
 */
@SpringBootTest
public class StringTest {
    @Test
    public void myTest() throws FileNotFoundException {

        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("aaa1");
        stringBuilder.append(1);
        stringBuilder.append(44.0);
        stringBuilder.append(33.00);
        System.out.println("反转前"+stringBuilder);
        //字符串反转
        stringBuilder.reverse();
        System.out.println("反转后"+stringBuilder);

        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("(这是测试数据");
        stringBuffer.append(11234567);

        try{
            FileReader fileWriter=new FileReader("C:\\Users\\Administrator\\Desktop\\1.txt");
            BufferedReader bufferedReader=new BufferedReader(fileWriter);
            char[] c=new char[20];
            while(bufferedReader.read(c)>0){
                    stringBuffer.append(c);
            }
            FileWriter fileWriter1=new FileWriter("C:\\Users\\Administrator\\Desktop\\2.txt");
            BufferedWriter bufferedWriter=new BufferedWriter(fileWriter1);
            bufferedWriter.write(new String(stringBuffer));
            bufferedWriter.flush();

            bufferedReader.close();
            bufferedWriter.close();

        }catch(Exception e){
            e.printStackTrace();
        }

        stringBuffer.append("------------------结束)");
        stringBuffer.reverse();
        System.out.println("#####3333###############:"+stringBuffer);
        System.out.println();
        //当反转之后,字符串一直处于反转状态,反转回来
        stringBuffer.reverse();
        System.out.println("从第十个字符串开始截取:"+stringBuffer.substring(10));
    }
}

 

 1 package com.example.demo.StringTest;
 2 import org.junit.jupiter.api.Test;import org.springframework.boot.test.context.SpringBootTest;
 3 import java.io.*;
 4 /** * @author hanqiang * @Classname StringTest * @Date 2021/5/20 14:23 * @Description: */@SpringBootTestpublic class StringTest {    @Test    public void myTest() throws FileNotFoundException {
 5         StringBuilder stringBuilder=new StringBuilder();        stringBuilder.append("aaa1");        stringBuilder.append(1);        stringBuilder.append(44.0);        stringBuilder.append(33.00);        System.out.println("反转前"+stringBuilder);        //字符串反转        stringBuilder.reverse();        System.out.println("反转后"+stringBuilder);
 6         StringBuffer stringBuffer=new StringBuffer();        stringBuffer.append("(这是测试数据");        stringBuffer.append(11234567);
 7         try{            FileReader fileWriter=new FileReader("C:\\Users\\Administrator\\Desktop\\1.txt");            BufferedReader bufferedReader=new BufferedReader(fileWriter);            char[] c=new char[20];            while(bufferedReader.read(c)>0){                    stringBuffer.append(c);            }            FileWriter fileWriter1=new FileWriter("C:\\Users\\Administrator\\Desktop\\2.txt");            BufferedWriter bufferedWriter=new BufferedWriter(fileWriter1);            bufferedWriter.write(new String(stringBuffer));            bufferedWriter.flush();
 8             bufferedReader.close();            bufferedWriter.close();
 9         }catch(Exception e){            e.printStackTrace();        }
10         stringBuffer.append("------------------结束)");        stringBuffer.reverse();        System.out.println("#####3333###############:"+stringBuffer);        System.out.println();        //当反转之后,字符串一直处于反转状态,反转回来        stringBuffer.reverse();        System.out.println("从第十个字符串开始截取:"+stringBuffer.substring(10));    }}
11 
12  

 

posted @ 2021-05-20 22:47  林鱼  阅读(51)  评论(0编辑  收藏  举报