----------android培训、java培训、java学习型技术博客、期待与您交流!------------
在此,分享一下自己学习JAVA的学习心得。有不对的地方请帮忙改正,也希望对想学java的同学有帮助!
JAVA基础
—IO流
在String类中,有可以进行编码和解码的方式:
1.编码:把看得懂的变成看不懂的
String.getBytes() --> byte[](编码后的一个数组)
------------------------------------------------------
byte[] getBytes() :使用平台默认的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。
byte[] getBytes(String charsetName): 使用指定的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。
2.解码:把看不懂的变成看得懂的
byte[] --> String
-------------------------------------------------------
通过String类的构造函数:
String(byte[] bytes, int offset, int length, String charsetName): 构造一个新的 String,方法是使用指定的字符集解码字节的指定子数组。
String(byte[] bytes, String charsetName):构造一个新的 String,方法是使用指定的字符集解码指定的字节数组。
导致乱码:通常是由于编码与解码的方式不一致;
解决方法:只要保证编码和解码保持一致,就不会出现乱码。
构造函数:
1.public OutputStreamWriter(OutputStream out):使用默认编码。接收字节流OutputStream(抽象)对象。我们学过的FileOutputStream
2.public OutputStreamWriter(OutputStream out,String charsetName):charsetName指定字符集;
写入数据:
1.void write(char[] cbuf):写入字符数组。
2.void write(char[] cbuf, int off, int len): 写入字符数组的某一部分。
3.void write(int c):写入单个字符。
4.void write(String str):写入字符串。 ---使用最方便的
5.void write(String str, int off, int len) :写入字符串的某一部分。
构造方法:
1.public InputStreamReader(InputStream in):使用平台默认字符集解码。参数InputStream(抽象)对象,我们使用FileInputStream
2.public InputStreamReader(InputStream in,String charsetName):使用指定字符集解码
读取的方法:
1.int read() : 读取单个字符。
2.int read(char[] cbuf) : 将字符读入数组。
3.int read(char[] cbuf, int off, int len) :将字符读入数组的某一部分。
4.int read(CharBuffer target) :试图将字符读入指定的字符缓冲区。
public class Demo {
public static void main(String[] args) {
//1.实例化输入、输出流
try {
//文件必须存在
InputStreamReader in = new InputStreamReader(new FileInputStream("a.txt"));
//文件可以不存在
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("b.txt"));
//方式一:一次读写一个字符
/*int n = 0;
while((n = in.read())!= -1){
out.write(n);
out.flush();
}*/
//方式二:一次读写一个字符数组
char[] charArray = new char[1024];
int len = 0;
while((len = in.read(charArray))!= -1){
out.write(charArray,0,len);
out.flush();
}
//释放资源:
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
操作流程:
1、创建相应的流对象,该对象被初始化时必须要明确被操作的文件。而且该文件会被创建到指定
目录下。如果该目录下已有同名文件,将被覆盖(调用第一个构造函数)。
2、进行相应的读写操作;
3、是否需要进行刷新操作,如果需要,则需要进行刷新;
4、关闭流,释放资源。
Java代码:
public class Demo {
public static void main(String[] args) {
// 1.构造输入输出流
try {
// 读取时,文件必须存在
FileReader in = new FileReader("b.txt");
// 输出流,文件可以不存在
FileWriter out = new FileWriter("c.txt");
// 复制
// 方式一:一次读取一个字符
/*
* int n = 0; while((n = in.read()) != -1){ out.write(n);
* out.flush(); }
*/
// 方式二:一次读取一个字符数组
char[] charArray = new char[1024];
int len = 0;
while ((len = in.read(charArray)) != -1) {
out.write(charArray, 0, len);
out.flush();
}
// 释放资源
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出流:BufferedWriter:将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
构造函数: BufferedWriter(Writer out): 创建一个使用默认大小输出缓冲区的缓冲字符输出流。
输入流:BufferedReader:从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。
构造函数:BufferedReader(Reader in): 创建一个使用默认大小输入缓冲区的缓冲字符输入流。
1.输出流:BufferedWriter: newLine():输出一个换行;
2.输入流:BufferedReader: readLine():一次读取一行数据;如果到达行末尾,返回null
public class Demo {
public static void main(String[] args) {
// 1.实例化输入输出流
try {
BufferedReader in = new BufferedReader(new FileReader("a.txt"));
BufferedWriter out = new BufferedWriter(new FileWriter("f.txt"));
// 一次读取一行数据,然后写入
String row = null;
while ((row = in.readLine()) != null) {
out.write(row);
// out.write("\r\n");
out.newLine();// 输出一个换行符
out.flush();
}
// 释放资源
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
TreeSet<Student> stuSet = new TreeSet<>(new Comparator() {
public int compare(Object o1, Object o2) {
Student stu1 = (Student) o1;
Student stu2 = (Student) o2;
// 比较总分:降序
int sum = stu2.getSum() - stu1.getSum();
// 如果总分相同,排语文分,升序
int num1 = (sum == 0 ? stu1.getChinese() - stu2.getChinese()
: sum);
// 如果语文相同,排数学
int num2 = (num1 == 0 ? stu1.getMath() - stu2.getMath() : num1);
// 如果数学相同,排英语
int num3 = (num2 == 0 ? stu1.getEnglish() - stu2.getEnglish()
: num2);
// 如果数学也相同;排姓名
int num4 = (num3 == 0 ? stu1.getName()
.compareTo(stu2.getName()) : num3);
return num4;
}
});
for (int i = 1; i <= 5; i++) {
System.out.println("请输入第 " + i + " 名学员信息:");
System.out.print("姓名:");
String name = sc.next();
System.out.print("语文:");
int chinese = sc.nextInt();
System.out.print("数学");
int math = sc.nextInt();
System.out.print("英语:");
int english = sc.nextInt();
// 封装Student对象
Student stu = new Student(name, chinese, math, english);
// 添加到集合
stuSet.add(stu);
}
// 遍历集合
System.out.println("遍历集合:");
System.out.println("\t姓名\t总分\t语文\t数学\t英语");
for (Student stu : stuSet) {
System.out.println("\t" + stu.getName() + "\t" + stu.getSum()
+ "\t" + stu.getChinese() + "\t" + stu.getMath() + "\t"
+ stu.getEnglish());
}
// 将集合写入文件
try {
if (saveToFile(stuSet)) {
System.out.println("数据已保存!");
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("写入数据时发生异常,请于系统管理员联系!");
}
}
private static boolean saveToFile(TreeSet<Student> stuSet)
throws IOException {
BufferedWriter out = new BufferedWriter(new FileWriter("student.txt"));
out.write("\t姓名\t总分\t语文\t数学\t英语");
out.newLine();
for (Student stu : stuSet) {
out.write("\t" + stu.getName() + "\t" + stu.getSum() + "\t"
+ stu.getChinese() + "\t" + stu.getMath() + "\t"
+ stu.getEnglish());
out.newLine();
out.flush();
}
out.close();
return true;
}
}
Student类:
public class Student {
private String name;
private int chinese;
private int math;
private int english;
public Student() {
super();
}
public Student(String name, int chinese, int math, int english) {
super();
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
//获取总分的方法
public int getSum(){
return this.chinese + this.math + this.english;
}
}
键盘录入方法:
读取转换流: BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
输出转换流:BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));//创建键盘输入流对象
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));//创建流对象,输出到控制台
String line = null;
while((line=bufr.readLine())!=null) {//读取一整行数据
if("over".equals(line))
break;
bufw.write(line.toUpperCase());//变为大写字母
bufw.newLine(); //
bufw.flush();//刷新
}
bufr.close();//释放资源
}
装饰设计模式:
当想要对已有的对象进行功能增强时,可以定义一个类,将已有对象传入,基于已有的功能,并提供强功能。那么自定义的该类称为装饰类。
继承和装饰的区别:
1)装饰模式比继承更灵活,避免了继承体系的臃肿,而且降低了类与类之间的关系。
2)装饰类因为增强已有对象,具备的功能和已有对象是相同的,只不过提供了更强的功能,所以装饰
3)类和被装饰类通常属于同一个体系中。
class Person
{
public void chifan()
{
System.out.println("吃饭");
}
}
class SuperPerson
{
private Person p ;
SuperPerson(Person p)
{
this.p = p;
}
public void superChifan()
{
System.out.println("开胃酒");
p.chifan();
System.out.println("甜点");
System.out.println("来一根");
}
}
class PersonDemo
{
public static void main(String[] args)
{
Person p = new Person();
//p.chifan();
SuperPerson sp = new SuperPerson(p);
sp.superChifan();
}
}