import java.io.*;public class TextStatistics { private String inputFileName; private String outputFileName; private int numChars; private int numWords; public TextStatistics(String inputFile, String outputFile) { inputFileName = inputFile; outputFileName = outputFile; numChars = 0; numWords = 0; } public void count() { try { BufferedReader reader = new BufferedReader(new FileReader(inputFileName)); String line; while ((line = reader.readLine()) != null) { numChars += line.length(); String[] words = line.split(" "); numWords += words.length; } reader.close(); } catch (IOException e) { e.printStackTrace(); } } public void output(boolean toFile) { String output = "Number of characters: " + numChars + "\n"; output += "Number of words: " + numWords + "\n"; if (toFile) { try { BufferedWriter writer = new BufferedWriter(new FileWriter(outputFileName)); writer.write(output); writer.close(); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println(output); } }}public class TextStatisticsTest { public static void main(String[] args) { System.out.println("Welcome to Text Statistics!"); System.out.println("Please enter the name of the input file: "); Scanner scanner = new Scanner(System.in); String inputFile = scanner.nextLine(); System.out.println("Please enter the name of the output file: "); String outputFile = scanner.nextLine(); System.out.println("Do you want to output to a file? (Y/N)"); boolean toFile = scanner.nextLine().equalsIgnoreCase("Y"); TextStatistics stats = new TextStatistics(inputFile, outputFile); stats.count(); stats.output(toFile); }}
时间: 2024-02-15 17:30:52 浏览: 156
这段代码是用 Java 编写的一个文本统计程序,可以统计输入文件中的字符数和单词数,并将结果输出到控制台或输出文件中。程序包括两个类,TextStatistics 类和 TextStatisticsTest 类。TextStatistics 类用于实现文本统计功能,其中 count() 方法用于统计字符数和单词数,output() 方法用于输出结果到控制台或输出文件中。TextStatisticsTest 类用于测试 TextStatistics 类的功能,其中通过控制台输入输入文件名、输出文件名和是否输出到文件的选项,并调用 TextStatistics 类的方法进行统计和输出。
相关问题
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class xhj { public static void main(String[] args) { if (args.length != 2) { System.out.println("请输入正确的输入流和输出流文件名!"); return; } String inputFileName = args[0]; String outputFileName = args[1]; InputStream inputStream = null; OutputStream outputStream = null; try { File inputFile = new File(inputFileName); inputStream = new FileInputStream(inputFile); File outputFile = new File(outputFileName); outputStream = new FileOutputStream(outputFile); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } System.out.println("文件复制成功!"); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }该代码中涉及的Java知识
该代码涉及的Java知识包括:
1. 文件操作:使用 File 类创建输入和输出文件对象,使用 FileInputStream 和 FileOutputStream 类读取和写入文件。
2. 输入输出流:使用 InputStream 和 OutputStream 类读取和写入数据。
3. 字节数组:使用 byte 数组作为缓冲区,读取和写入文件的数据。
4. 异常处理:使用 try-catch-finally 语句处理文件读写可能出现的异常,确保操作完成后关闭输入输出流。
5. 命令行参数:使用 args[] 数组获取用户在命令行中输入的输入流和输出流文件名。
int main(int argc, char* argv[]) { string inputFileName = "..\\testData\\temp2.dat"; string outputFileName = inputFileName; FILE* pf = NULL; unsigned char * datBuffer = NULL; unsigned char * imageBuffer = NULL; int inputWidth = 2560; int inputHeight = 2560; int i = 0, j = 0; int indexX = 0, indexY = 0; unsigned short tempPixel = 0; unsigned char tempByte; GDALDriver* pDriver = NULL; GDALDataset* pDs = NULL; char **papszOptions = NULL; char szFormat[16]; GDALDataType nDataType; GDALAllRegister(); datBuffer = (unsigned char *)malloc(inputWidth*inputHeight * 2); imageBuffer = (unsigned char *)malloc(inputWidth *inputHeight * 2); pf = fopen(inputFileName.c_str(), "rb"); fread(datBuffer, 1, inputWidth*inputHeight * 2, pf); fclose(pf); for (i = 0; i < inputHeight; i++) { for (j = 0; j < inputWidth; j++) { indexX = i; indexY = j; ////左右翻转 //indexX = i; //indexY = inputWidth -1 -j; //取一个像素 tempByte = *(datBuffer + (i*inputWidth + j) * 2); tempPixel = tempByte << 8; tempByte = *(datBuffer + (i*inputWidth + j) * 2 + 1); tempPixel += tempByte; tempPixel = tempPixel >> 6; //写回像素,低字节在前 *(imageBuffer + (indexX*inputWidth + indexY) * 2) = tempPixel & 0xFF; *(imageBuffer + (indexX*inputWidth + indexY) * 2 + 1) = (tempPixel & 0xFF00) >> 8; } } //写为文件 outputFileName = inputFileName + ".test.tiff"; papszOptions = CSLAddString(papszOptions, "COMPRESS=LZW"); nDataType = GDT_UInt16; strcpy(szFormat, "GTIFF"); pDriver = (GDALDriver*)GDALGetDriverByName(szFormat); if (pDriver != NULL) { pDs = pDriver->Create(outputFileName.c_str(), inputWidth, inputHeight, 1, nDataType, papszOptions); if (pDs != NULL) { pDs->GetRasterBand(1)->RasterIO(GF_Write, 0, 0, inputWidth, inputHeight, imageBuffer, inputWidth, inputHeight, nDataType, 0, 0); GDALFlushCache(pDs); GDALClose(pDs); } } free(datBuffer); free(imageBuffer); return 0; }
这段代码是一个C++程序,主要功能是读取一个.dat文件中的数据,并将数据写入一个.tiff文件中。文件路径和文件名都是通过输入参数来指定的。具体流程如下:
1. 首先,程序定义了一些变量,包括输入文件名(inputFileName)、输出文件名(outputFileName)、文件指针(pf)、数据缓冲区(datBuffer和imageBuffer)、输入图像的宽度和高度(inputWidth和inputHeight)、一些循环中使用的索引变量(i、j、indexX和indexY)、临时像素值变量(tempPixel)以及临时字节变量(tempByte)。
2. 然后,程序调用GDALAllRegister()函数进行GDAL库的初始化操作。
3. 接下来,程序通过调用fopen()函数打开输入文件,并使用fread()函数将文件中的数据读入到datBuffer中,然后再调用fclose()函数关闭文件。
4. 然后,程序通过两个嵌套的循环遍历图像的每个像素,其中i表示行索引,j表示列索引。在每个像素处,程序首先计算出在imageBuffer中的索引位置(根据是否进行左右翻转),然后从datBuffer中读取两个字节的数据,拼接成一个16位的像素值(tempPixel),并将其右移6位以进行取样操作。最后,程序将取样后的像素值按照低字节在前的方式写入到imageBuffer中。
5. 循环结束后,程序将imageBuffer中的数据写入到输出文件中。首先,程序构造输出文件名(在输入文件名后加上".test.tiff"后缀),然后使用GDAL库提供的函数创建输出文件,并设置其宽度、高度、波段数、数据类型和压缩选项。接下来,程序通过调用GetRasterBand()函数获取输出文件的第一个波段,并使用RasterIO()函数将imageBuffer中的数据写入到输出文件中。最后,程序调用GDALFlushCache()函数刷新缓存,并调用GDALClose()函数关闭输出文件。
6. 最后,程序释放datBuffer和imageBuffer的内存,并返回0表示运行成功。
请注意,这段代码涉及到了GDAL库的使用,需要在编译环境中引入相应的头文件和链接相应的库文件才能正常编译和运行。
阅读全文
相关推荐
![text/x-java](https://img-home.csdnimg.cn/images/20250102104920.png)
![text/x-java](https://img-home.csdnimg.cn/images/20250102104920.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)