Java 如何编写文件处理问题的代码

Java 如何编写文件处理问题的代码,java,Java,假设我们有一个文本文件,其字符数少于500个,假设我们有300个字符。现在,您必须通过追加文本文件500-300=200的前200个字符来生成500个字符。如何在Java中使用文件处理实现这一点 代码: while (((data = br.readLine()) != null) { count = count + data.length(); tstring = tstring + data; } if (count < 500) { String tstri

假设我们有一个文本文件,其字符数少于500个,假设我们有300个字符。现在,您必须通过追加文本文件500-300=200的前200个字符来生成500个字符。如何在Java中使用文件处理实现这一点

代码:

while (((data = br.readLine()) != null) {
    count = count + data.length();
    tstring = tstring + data;
}

if (count < 500) {
    String tstring2 = tstring.substring(1, count);
}

我不知道在此之后如何使用现有字符串生成长度为500的字符串。

首先需要一个整数变量来保存要生成的字符串的大小。您最初给出了一个示例,用该值初始化变量:

// Desired length of string to create from file contents.
int desiredStringLength = 500;
根据提供给desiredStringLength变量的内容,将确定代码成功构建字符串所需的文本文件中允许的最小字符内容。这是必需的,因为根据您的帖子,要构建字符串,从文件开始只允许一次采集,并且文件中必须有足够的字符来处理业务。此允许的最小文件内容值通过获取提供的desiredStringLength并将其除以2来确定。但是,如果除法留下余数,则除法计算中添加1:

/* Calculate and hold the MINIMUM number of characters 
   allowed to be in the supplied text file. A Ternary
   Operator is used to select the appropriate calculation.  */ 
int minimumFileLength = (desiredStringLength % 2 == 0 ? 
                         desiredStringLength/2 : 
                         (desiredStringLength/2) + 1);
现在,您只需获取文本文件的内容并将其内容放入单个字符串变量中:

String fileAsString = ""; // Holds all the file data as a single string.
try {    
    fileAsString = new String(Files.readAllBytes(Paths.get("test2.txt")));
}
catch (IOException ex) {
    // Inform of any errors then exit method
    System.err.println("File Error! --> " + ex.getMessage());
    return;
}
现在fileAsString变量将文本文件的内容作为单个字符串保存。现在的问题只是用你喜欢的任何方法拉出你想要的角色。整个代码可能如下所示:

String ls = System.lineSeparator(); // OS dependent 'System Line Break' character(s).
int desiredStringLength = 500;      // Desired length of string to create from file contents.

/* Calculate and hold the MINIMUM number of characters 
   allowed to be in the supplied text file. A Ternary
   Operator is used to select the appropriate calculation.  */ 
int minimumFileLength = (desiredStringLength % 2 == 0 ? 
                        desiredStringLength/2 : 
                        (desiredStringLength/2) + 1);

String fileAsString = ""; // Holds all the file data as a single string.
String finalString = "";  // Will hold the string which will be built.

// Display some information...
System.out.println("Desired length of String to create from File: --> " + desiredStringLength);
System.out.println("Minimum 'Allowable' Character count in file: ---> " + minimumFileLength);

// Place the entire file contents into the fileAsString variable.
try {    
    fileAsString = new String(Files.readAllBytes(Paths.get("test2.txt")));
}
catch (IOException ex) {
    // Inform of any errors then exit method
    System.err.println("File Error! --> " + ex.getMessage());
    return;
}

// More informational display...
System.out.println("Actual character count in file: ----------------> " + fileAsString.length());
System.out.println(ls + "The Created String:");
System.out.println("===================");

// If the file contents is less the the minimum allowable then 
// Inform User and exit method.
if (fileAsString.length() < minimumFileLength) {
    System.err.println("File Content Error! The supplied file does not " +
                       "contain enough content to build a string " + desiredStringLength + 
                       " characters long!" + ls + "File content " + "total: --> " + 
                       fileAsString.length() + " characters");
    return;
}
/* If the file content count is less than the desired build 
   string length then subtract what is contained in file from 
   the desired string build size and pull the calculated amount 
   from the beginnig of file content and append it to the file 
   content. This is all placed into the finalString variable.  */
else if (fileAsString.length() <= desiredStringLength) {
    finalString += fileAsString + fileAsString.substring(0, desiredStringLength-fileAsString.length());
}
/* If the file content count is at the desired string build size
   or greater then just build the desired string from the beginning
   of the file content.       */
else {
    finalString = fileAsString.substring(0, desiredStringLength);
}

// Display the built string.
System.out.println(finalString);
// Inform User of built string length.
System.out.println(ls + "Created String Length: --> " + finalString.length());

您可以读取整个内容并将其视为单个字符串。读取该文件,附加其前200个字符,然后再次写入该文件。请添加您迄今为止尝试过的程序,好吗?因此,我们可以帮助您纠正错误。这个问题的目的是什么?您现有的系统是否需要这个案例,或者您是否正在练习它以获得JavaShow方面的知识?我编辑了这篇文章