文本输入与输出
2018年11月22日 星期四
下午5:57
简述大致方法,纯属个人笔记
InputStreamReader in = new InputSreamReader(new FileInputStream(“pathname”), “utf-8”);
编码模式utf-8
PrintWriter out = new PrintWriter(“pathname”, true)
等同于PrintWriter out = new PrintWriter(new FileWriter(“pathname”), true) ture是启用自动冲刷缓冲区
System.getProperty(“line.separator”) 获得系统换行符的字符串
java.io.PrintWriter
PrintWriter(Writer out)
PrintWriter(Writer out, boolean autoFlush)
PrintWriter(OutputStream out)
PrintWriter(OutputStream out, boolean autoflush)
PrintWriter(String filename)
PrintWriter(File file)
void print(Object obj) 打印从toString产生的字符串
void print(String s)
void println(String s) 当流处于自动冲刷时,会冲刷这个流
void print(char[] s)
void print(int i)
void print(long l)
void print(float f)
void print(double d)
void print(boolean b)
void print(String format, Object…args) 按照格式打印给定值
boolean checkError() 如果产生格式化输出错误,则返回true
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(“pathname”), “utf-8”));
String line;
while((line = in.readLine()) != null)
{
//do something
}
BufferedReader不能读数字,Scanner能读数字
String.split(“1”) 将字符串按照1分隔去掉1, 遇到"|“时用转义”\|“因为转义”|"还须转义
Scanner.nextInt() 返回值利用
创建编码方式
Charset cset = Charset.forName(“ISO-8859-1”);
遍历字符集
Map<String, Charset> charsets = Charset.availableCharset();
for (String name : charsets.keySet())
System.out.println(name);
编码和解码
String str = " "
ByteBuffer buffer = cset.encode(str);
byte[] bytes = buffer.array();
ByteBuffer bbuf = ByteBuffer.wrap(bytes, offset, length);
CharBuffer cbuf = cset.decode(bbuf);
String str = cbuf.toString();
ByteBuffer.warp是将一个字节数组转换成一个字节缓存区
java.nio.charset.Charset
static SortedMap availableCharsets() 获取所有可用的字符集。返回一个映射表,键是字符集名,值是字符集
static Charset forName(String name) 获取给定名称的字符集
Set aliases() 返回这个字符集的别名称
ByteBuffer encode(String str) 编码
CharBuffer decode(ByteBuffer buffer) 解码
java.nio.ByteBuffer
byte[] array() 返回缓冲区内的值,存入字节数组
static ByteBuffer wrap(byte[] bytes)
static ByteBuffer wrap(byte[] bytes, int offset, int length) 字节数组转换为ByteBuffer
java.nio.CharBuffer
char[] array()
char charAt(int index)
String toString()