原始数据为几分txt,里面是采集的浮点型数据,用于绘制趋势图,按小时为单位导出,每小时大概18万个点。
绘制趋势时,需要将几个小时的采样点都提取出来,并且需要进行平滑处理(存在部分噪点数据,通过平均处理,将特别高或者特别低的数据对趋势图的影响降低,平滑趋势)最终输出一个又均值组成的txt。
package com.fpi.text;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
*
* @ClassName: App
* @Description:合并TXT
* @author: luchenxi 18163
* @date: 2020年7月21日 下午3:46:12
* @Copyright: FPI
*/
public class App
{
public static void main(String[] args)
{
try
{
// 静态量记录时间
//float index = 0;
App app = new App();
// 获取当前目录
String path = app.getCourrentPath();
// 创建新文件
File file = new File(path + "/all.txt");
if(!file.exists())
{
file.createNewFile();
}
else
{
file.delete();
file.createNewFile();
}
// 获取文件夹目录
String txtFileDir = path + "/target";
// 获取目标目录下所有文件
File txtDir = new File(txtFileDir);
if(txtDir.exists())
{
File[] tFiles = txtDir.listFiles();
for(int i = 0 ; i < tFiles.length ; i++)
{
System.out.println(tFiles[i].getName() + " ...");
List<String> contents = App.readFileContent(tFiles[i]);
double value = 0;
for(int j = 0 ; j < contents.size() ; j++)
{
if(j > 0 && j % 50 == 0)
{
double finalval = (value / 50);
String line = String.valueOf(finalval);
App.writeData(line, file);
value = 0;
}
value = Double.valueOf(contents.get(j)) + value;
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
*
* @Title: getCourrentPath @Description: 当前路径 @param: @return @return:
* String @throws
*/
public String getCourrentPath()
{
String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
int firstIndex = path.lastIndexOf("/") + 1;
path = path.substring(1, firstIndex);
return path;
}
/**
*
* @Title: readFileContent @Description: 读文件 @param: @param
* file @param: @return @return: List<String> @throws
*/
public static List<String> readFileContent(File file)
{
BufferedReader reader = null;
List<String> contents = new ArrayList<String>();
try
{
reader = new BufferedReader(new FileReader(file));
String tempStr;
while((tempStr = reader.readLine()) != null)
{
String[] tabs = tempStr.split(" ");
contents.add(tabs[1]);
}
reader.close();
return contents;
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if(reader != null)
{
try
{
reader.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
return contents;
}
public float nextIndex(float index)
{
float add = 0.02f;
BigDecimal b1 = new BigDecimal(Float.toString(index));
BigDecimal b2 = new BigDecimal(Float.toString(add));
return b1.add(b2).floatValue();
}
public static void writeData(String content , File file)
{
content = content + "\r\n";
try
{
FileOutputStream fos = new FileOutputStream(file, true);
fos.write(content.getBytes());
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}