package com.mycim.webapp.vanchip;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.mycim.core.util.StringUtils;
import com.mycim.wip.model.Die;
import jcifs.smb.SmbFile;
import liquibase.util.csv.opencsv.CSVReader;
public class testTextController {
public static void main(String[] args)
{
String readtxtfilepath = "D:\\19C\\T7MC202R6T_01_221205111955map.txt";
String readExcelfilepath = "D:\\19C\\T7MC202R6T_01_221205111955.xls";
int n=GetRowCount1(readtxtfilepath);
System.out.println("共有"+n+"行数据");
double[][] mypoint = new double[n][2];
for(int i=0;i<n;i++)
{
mypoint[i][0]=0;
mypoint[i][1]=0;
}
readCpExcel(readExcelfilepath);
//ReadTxt1S(readtxtfilepath, mypoint);
System.out.println("Hello World!");
// WriteTxt(writetxtfilepath,mypoint);
// for(int i=0;i<n;i++)
// System.out.println((i+1)+":["+mypoint[i][0]+","+mypoint[i][1]+"]");
}
public static void WriteTxt(String txtPath,double[][] p){
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(txtPath);//创建文本文件
for(int i=0;i<p.length;i++)
{
if(i == p.length -1) fileWriter.write(p[i][0]+","+p[i][1]);
else fileWriter.write(p[i][0]+","+p[i][1]+"\r\n");
}
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void ReadTxt1S(String txtPath,double[][] mypoint) //读取txt中的经纬度并赋值给Point数组
{
File file = new File(txtPath);
if(file.isFile() && file.exists())
{
try
{
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line = null;
int row = 1;
int length;
ArrayList<String> result = new ArrayList<>();
StringBuilder stringBuilder = new StringBuilder();
char achar;
LinkedList<String> lineResult = new LinkedList<>();
int start = 0;
boolean flag;
while ((line = bufferedReader.readLine()) != null) {
if (line.startsWith("-")) {
flag = (start % 2 == 0);
length = line.length();
for (int i = 0; i < length; i++) {
achar = line.charAt(i);
if (Character.isDigit(achar)) {
stringBuilder.append(achar).append(",").append(row).append(",").append(i + 1);
if (flag) {
lineResult.add(stringBuilder.toString());
} else {
lineResult.addFirst(stringBuilder.toString());
}
stringBuilder.setLength(0);
}
}
row += 1;
start += 1;
result.addAll(lineResult);
lineResult.clear();
}
}
System.out.println(result.toString());
bufferedReader.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
public static void readCpExcel(String txtPath)
{
File file = new File(txtPath);
Workbook workbook = null;
if(file.isFile() && file.exists())
{
try
{
String line = "";
String SplitBy = " ";
String[] Line;
boolean flag = false;
List<String[]> BikeDataList = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(txtPath))) {
int k = 0;
while ((line = br.readLine()) != null) {
Line = line.split(SplitBy);
BikeDataList.add(Line);
// System.out.println("signal "+Line[0]);
if (!flag &&Line[0].contains("Serial#")) {
flag = true;
System.out.println("BIN "+Line[0]);
continue;
}
if (flag) {
if (k < 9){
String liness = Line[1].replaceAll("\t"," ").trim();
String[] array = liness.split(" ");
System.out.println("BIN "+array[2]+" X:"+array[4]+" Y:"+array[5]);
} else {
String liness = Line[0].replaceAll("\t"," ").trim();
String[] array = liness.split(" ");
System.out.println("BIN "+array[2]+" X:"+array[4]+" Y:"+array[5]);
}
k++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
//readExcel(new FileInputStream(file),file.getName());
} catch (Exception e)
{
e.printStackTrace();
}
}
}
//这个下面是单独函数
public static Workbook readExcel(InputStream is,String extString){
Workbook wb = null;
extString = extString.substring(extString.lastIndexOf("."));
try {
if(".xls".equals(extString)){
return wb = new HSSFWorkbook(is);
}else if(".xlsx".equals(extString)){
return wb = new XSSFWorkbook(is);
}else {
return wb = null;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return wb;
}
public static void ReadTxt1(String txtPath,double[][] mypoint) //读取txt中的经纬度并赋值给Point数组
{
File file = new File(txtPath);
if(file.isFile() && file.exists())
{
try
{
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line = null;
int row = 1;
int length;
ArrayList<String> result = new ArrayList<>();
StringBuilder stringBuilder = new StringBuilder();
char achar;
while( (line=bufferedReader.readLine())!=null){
if (line.startsWith("-")) {
length = line.length();
for (int i = 0; i < length; i++) {
achar = line.charAt(i);
if (Character.isDigit(achar)) {
stringBuilder.append(achar).append(",").append(row).append(",").append(i+1);
System.out.println(stringBuilder.toString());
result.add(stringBuilder.toString());
stringBuilder.setLength(0);
}
}
}
row +=1;
}
bufferedReader.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
public static List<String> dealFile(BufferedReader bufferedReader) throws Exception {
int row = 1;
String line;
int length;
ArrayList<String> result = new ArrayList<>();
StringBuilder stringBuilder = new StringBuilder();
char achar;
LinkedList<String> lineResult = new LinkedList<>();
int start = 0;
boolean flag;
while ((line = bufferedReader.readLine()) != null) {
if (line.startsWith("-")) {
flag = (start % 2 == 0);
length = line.length();
for (int i = 0; i < length; i++) {
achar = line.charAt(i);
if (Character.isDigit(achar)) {
stringBuilder.append(achar).append(",").append(row).append(",").append(i + 1);
if (flag) {
lineResult.add(stringBuilder.toString());
} else {
lineResult.addFirst(stringBuilder.toString());
}
stringBuilder.setLength(0);
}
}
start += 1;
result.addAll(lineResult);
lineResult.clear();
}
row += 1;
}
return result;
}
public static int GetRowCount1(String txtpath)
{
File file = new File(txtpath);
int rowcount=0;
if(file.isFile() && file.exists())
{
try
{
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String text = null;
while((text = bufferedReader.readLine()) != null)
{
rowcount++;
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return rowcount;
}
public static String[] split(String lineTxt) {
String[] arr = lineTxt.split(" ");
//去除空元素
List<String> list = new ArrayList<>();
for (String str : arr) {
if (StringUtils.isNotEmpty(str)) {
list.add(str);
}
}
return list.toArray(new String[0]);
}
}
java读取text&excel测试类
最新推荐文章于 2024-06-02 23:31:25 发布