【问题标题】:JAVA Writing to a file with specific offsetJAVA写入具有特定偏移量的文件
【发布时间】:2015-03-07 09:52:27
【问题描述】:

我必须编写一个程序来读取文件并插入用户通过控制台窗口提供的一些文本。插入文本的位置也应该通过控制台窗口给出。

下面是我的代码,输入句子和偏移量后出现“字符串索引超出范围”。

输入句子: 嘿嘿

输入位置: 5

字符串索引超出范围:9

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.FileInputStream; 
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.Scanner;
import java.io.Writer;


class ReadPosition{ 
    public static void main(String args[])    {   
    try{       
        FileWriter Writer =
                new FileWriter("in.txt");
        @SuppressWarnings("resource")
        BufferedWriter bufferedWriter =
                new BufferedWriter(Writer);


        Scanner input= new Scanner(System.in);
        System.out.println("Enter The Sentence: ");
        String sentence = input.nextLine();  
        System.out.println("Enter location: ");
        int offset = input.nextInt();
        input.close();

        byte[] buffer = sentence.getBytes();
        int len = buffer.length;
        bufferedWriter.write(sentence, offset, len);

    }

    catch(Exception ex)     
                { 
                    System.out.println(ex.getMessage());      
                } 


    }

}

【问题讨论】:

    标签: java io output fileinputstream


    【解决方案1】:

    线

    bufferedWriter.write(sentence, offset, len);
    

    表示从sentence 中写出len 字符,从offset 开始到sentence

    换句话说,offset 是句子中的位置不是输出文件中的位置。

    如果你想在文件中插入文本,你需要编写代码将文件复制到一个新文件,在复制过程中将文本添加到正确的位置。

    另外你不应该使用

    @SuppressWarnings("resource")
    

    要禁止关闭丢失的警告 - 你确实需要关闭你的作家。

    【讨论】:

    • 我明白你的意思,谢谢你帮助我。但是我怎样才能更正代码,以便我可以在输出文件中写句子?!
    • 如果你想插入文本而不是覆盖后面的内容,你必须复制文件。所以你的代码需要完全重写。
    【解决方案2】:

    Given 是在特定位置写入内容的方法。

    假设我的文件是Test.txt,内容如下

    Hello 
    How are you
    Today is Monday
    

    现在你想在你好之后写“hi”。所以“hi”的偏移量将是“5”。

    方法是:

    filename = "test.txt";
    offset = 5;
    byte[] content = ("\t hi").getBytes();
    
    private void insert(String filename, long offset, byte[] content) throws IOException {
    
    RandomAccessFile r = new RandomAccessFile(filename, "rw");
    RandomAccessFile rtemp = new RandomAccessFile(filename+"Temp", "rw");
    long fileSize = r.length(); 
    FileChannel sourceChannel = r.getChannel();
    FileChannel targetChannel = rtemp.getChannel();
    sourceChannel.transferTo(offset, (fileSize - offset), targetChannel);
    sourceChannel.truncate(offset);
    r.seek(offset);
    r.write(content);
    long newOffset = r.getFilePointer();
    targetChannel.position(0L);
    sourceChannel.transferFrom(targetChannel, newOffset, (fileSize - offset));
    sourceChannel.close();
    targetChannel.close();
    rtemp.close();
    r.close();
    

    }

    输出将是:

    Hello hi
    How are you
    Today is Monday
    

    【讨论】:

      【解决方案3】:

      这是因为内部write方法会调用getchar api来获取从offset(5)到offset + (sentence.length - offset)(9)的字符

      而你的句子只有 4 个字符长(即嘿嘿)。所以你应该检查你的代码,偏移量应该总是小于sentence.length,即其中的字符数。

      【讨论】:

      • 我不明白你的意思,你能在代码中告诉我吗?!
      • 参见this,尤其是s.getChars(b, b + d, cb, nextChar);,这就是它的作用,并试图在我的回答中解释。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 2013-08-24
      相关资源
      最近更新 更多