package com.jack.test;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Test {
public static String path = "aa.txt";
public static void main(String[] args) {
test1();
test2();
}
/**
* 从文本读取字符,压缩相邻相同字符
* @throws Exception
*/
//@org.junit.Test
public static void test1(){
String str = readStrByStream().trim();
Logger.getLogger(Test.class.getName()).log(Level.INFO,str);
String str2 = readStrByReader();
Logger.getLogger(Test.class.getName()).log(Level.INFO,str2);
String[] arr = str.split("");
int count = 1;
for(int i=0;i<arr.length-1;i++){
if(arr[i].equals(arr[i+1])){
count++;
if(i==arr.length-2)
System.out.print(count+arr[i]);
continue;
}
System.out.print(count+arr[i]);
count = 1;
if(i==arr.length-2)
System.out.print(count+arr[i+1]);
}
System.out.println();
}
/**
* 顺序列出每个单词出现的次数
*/
//@org.junit.Test
public static void test2(){
String str = "one two three one two three,one two three four";
Logger.getLogger(Test.class.getName()).log(Level.INFO,str);
String[] arr = str.split("[ ,]");
String[] sortArr = new String[arr.length];//排序起作用
Map<String,Integer> map = new HashMap<String,Integer>();//做数据对比,及计数
for(int i=0;i<arr.length;i++){
if(map.containsKey(arr[i])){
map.put(arr[i], map.get(arr[i])+1);
}else{
map.put(arr[i], 1);
sortArr[i] = arr[i];
}
}
for(String s:sortArr){
if(s==null) continue;
System.out.print(map.get(s)+s+",");
}
}
/*
*
*/
public static void test3(){
}
public static void show(String[] arr){
for(String s:arr)
System.out.print(s+" ");
}
/**
* 从文件中读取字符串
* @return
* @throws Exception
*/
@SuppressWarnings("resource")
private static String readStrByStream(){
StringBuilder build = new StringBuilder();
InputStream in = null;
try {
//in = new FileInputStream(new File(getFile()));
in = Test.class.getResourceAsStream(path);
byte[] bytes = new byte[1024];
while(in.read(bytes)!=-1){
build.append(new String(bytes));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return build.toString();
}
private static String readStrByReader(){
StringBuilder build = new StringBuilder();
BufferedReader reader = null;
try {
//reader = new BufferedReader(new FileReader(new File(Test.class.getResource(path).getFile())));
reader = new BufferedReader(new InputStreamReader(Test.class.getResourceAsStream(path)));
String str = "";
while((str = reader.readLine())!=null)
build.append(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return build.toString();
}
private static String getFilePath(String path){
//Test.class.getResourceAsStream(path).
return null;
}
public static String getFile(){
return Test.class.getResource(path).getFile();
}
}
字符串压缩
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 一、IO流的概念 Java的IO流是实现输入/输出的基础,它可以方便地实现数据的输入/输出操作,在Java中把不同...
- 字节流 InputStream 输入字节流 OutputStream 输出字节流 输入字节流----InputSt...
- 本篇文章主要介绍IO流的分类、概念以及IO流中的四大基类,通过对IO流中的框架体系进行介绍,对IO流有一个整体的了...