StringBuilder 类在 Java 5 中被提出,它和 StringBuffer 之间的最大不同在于 StringBuilder 的方法不是线程安全的(不能同步访问)。
由于 StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder 类。
String 覆盖了 equals 方法和 hashCode 方法,而 StringBuffer 没有覆盖 equals 方法和 hashCode 方法,
所以,将 StringBuffer 对象存储进 Java集合类中时会出现问题。
Scanner和BufferedReader的选择
用Scanner获得用户的输入非常的方便,但是Scanner取得输入的依据是空格符,包括空格键,Tab键和Enter键.当按下这其中的任一键时,Scanner就会返回下一个输入。当你输入的内容中间包括空格时,使用Scanner就不能完整的获得你输入的字符串。这时候我们可以考虑使用BufferedReader类取得输入。
缓冲区中的数据保存直到缓冲区满后才写出,也可以使用flush方法将缓冲区中的数据强制写出或使用close()方法关闭流, 关闭流之前,缓冲输出流将缓冲区数据一次性写出。
java 中 IO 流分为几种?
按功能来分:输入流(input)、输出流(output)。
按类型来分:字节流和字符流。
字节流和字符流的区别:
每次读写的字节数不同;字节流是只读取一个字节,字符流是读取2个字节。
字符流是块读写,字节流是字节读写;
字符流带有缓存,字节流没有。
java流在处理上分为字符流和字节流。字符流处理的单元为2个字节的Unicode字符,分别操作字符、字符数组或字符串,
而字节流处理单元为1个字节,操作字节和字节数组。
字符流和字节流,一个属性范围小,一个属性范围大,字符流只能是字符这一种类型,但是字节流中可以是字符,可以是二进制文件,可以是音频,可以是各种各样的类型,只要符合字节形式存储的都可以接字节流,而字符流只能接字符。
OutputstreamWriter,是writer的子类,将输出的字符流变为字节流 InputStreamReader,是Reader的子类,将输入的字节流变为字符流
使用 newline 函数换行,应该注意:
在本机测试的时候,因为是 windows 环境,换行符是 \r\n ,打开文件时候自然文件是换行处理,没有问题。
当我们部署到服务器时候,服务器是 linux 环境,newline 读取系统换行符是 \r ,导出到文件,
文件的换行符是 \r,当我们把这个文件通过浏览器下载到 windows 时候,再打开文件将会出现没有换行的问题。因为 windows 下对于 \r 的解释并不是换行符。
读取文本内容(使用FileReader)
FileReader fileReader = new FileReader(“C:/测试.txt”);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
读取文本内容(使用FileInputStream)
File file = new File(“C:/测试.txt”);
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,“UTF-8”);
BufferedReader reader = new BufferedReader(inputStreamReader);
Date date = new Date();
String temp = “”;
while ((temp = reader.readLine()) != null) {
System.out.println(temp);
}
fileInputStream.close();
inputStreamReader.close();
reader.close();
读取文件复制到另一目录
File file = new File(“C:/测试.docx”);
InputStream in = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(new File(“C:/测试2.docx”));
int read;
byte[] bytes = new byte[1024];
while ((read = in.read(bytes)) != -1) {
fos.write(bytes, 0, read);
}
in.close();
fos.close();
读取文件转成字节数组
File file = new File(“C:/测试.docx”);
InputStream inputStream = new FileInputStream(file);
ByteArrayOutputStream baos =new ByteArrayOutputStream();
try {
byte[] bytes =new byte[1024*4];
int n =0;
while((n = inputStream.read(bytes))!=-1){
baos.write(bytes,0, n);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
inputStream.close();
}
}
byte[] bytes = baos.toByteArray();
//字节数组转Base64
Base64.Encoder encoder = Base64.getEncoder();
String base64Str = encoder.encodeToString(bytes);
//Base64转字节数组
byte[] bytes2 = Base64.getDecoder().decode(base64Str);
压缩图片大小
@Test
public void test04() throws Exception {
File file = new File(原图片路径);
imageCompress(file.length(),原图片路径,压缩后图片路径);
}
public static boolean imageGenerateSmall(String oidPath, String newPath, double smallSize) {
try {
File bigFile = new File(oidPath);
Image image = ImageIO.read(bigFile);
int width = image.getWidth(null);
int height = image.getHeight(null);
int widthSmall = (int) (width / smallSize);
int heightSmall = (int) (height / smallSize);
BufferedImage buffi = new BufferedImage(widthSmall, heightSmall, BufferedImage.TYPE_INT_RGB);
Graphics g = buffi.getGraphics();
g.drawImage(image, 0, 0, widthSmall, heightSmall, null);
g.dispose();
ImageIO.write(buffi, "jpg", new File(newPath));
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 图片压缩,图片大小超过100K自动按比例压缩到100K以下
*
* @param fileSize 文件大小
* @param oidPath 原文件路径
* @param newPath 压缩后路径
* @return
* @throws Exception
*/
public static boolean imageCompress(long fileSize, String oidPath, String newPath) throws Exception {
boolean rs = true;
int kb = 100 * 1024;
if (fileSize > 100 * 1024) {
int smallSize = (int) (fileSize % kb == 0 ? fileSize / kb : fileSize / kb + 1);
double size = Math.ceil(Math.sqrt(smallSize));
rs = imageGenerateSmall(oidPath, newPath, size);
}
return rs;
}