英文:
How to swap the first word with the last word in the file in Java?
问题
我正在尝试将第一个单词移到Java中的最后一个位置。我可能遗漏了什么吗?
没有将第一个单词更改为最后一个单词,并且不确定是否然后将字符串再次添加到文件中。
public class WorkString {
    public WorkString() {
        this.reader = new BufferedReader(new InputStreamReader(System.in));
    }
    public void swapWords() throws IOException {
        System.out.println("输入文件名:");
        filename = reader.readLine();
        fio = new RandomAccessFile(new File(filename), "r");
        data = fio.readLine();
        System.out.println("来自文件的信息:" + data);
        String content = fio.toString();
        System.out.println(content);
        String[] ori = content.split(" ");
        String str = "";
        String temp = ori[0];
        ori[0] = ori[ori.length - 1];
        ori[ori.length - 1] = temp;
        for (int i = 1; i < ori.length; i++)
            str += ori[i];
        fio.writeBytes(str + ori[0]); // 我可以这样做吗?
        fio.close();
    }
}
英文:
I am trying to move the first word to the last position in Java.What could I be missing?
Doesn't change the first word to the last one and is not sure that then the String is added to the file again.
public class WorkString {
    public WorkString() {
        this.reader = new BufferedReader(new InputStreamReader(System.in));
    }
    public void swapWords() throws IOException {
        System.out.println("Enter file name : ");
        filename = reader.readLine();
        fio = new RandomAccessFile(new File(filename), "r");
        data = fio.readLine();
        System.out.println("Information from file : " + data);
        String content = fio.toString();
        System.out.println(content);
        String[] ori = content.split(" ");
        String str = "";
        String temp=ori[0];
        ori[0]=ori[ori.length-1];
        ori[ori.length-1]=temp;   
        for(int i = 1; i < ori.length; i++)
            str += ori[i];
        fio.writeBytes(str + ori[0]); // Can I do this?
        fio.close();
    }
}
答案1
得分: 0
public void swapWords() throws Exception {
    System.out.println("输入文件名 : ");
    String filename = reader.readLine();
    RandomAccessFile fio = new RandomAccessFile(new File(filename), "rw");
    String data = fio.readLine();
    System.out.println("文件中的信息 : " + data);
    String content = data;
    System.out.println(content);
    String[] ori = content.split(" ");
    String str = "";
    String temp = ori[0];
    ori[0] = ori[ori.length - 1];
    ori[ori.length - 1] = temp;
    str = String.join(" ", ori);
    fio.write(str.getBytes(StandardCharsets.UTF_8));
    fio.close();
}
英文:
public void swapWords() throws Exception {
	System.out.println("Enter file name : ");
	String filename = reader.readLine();
	RandomAccessFile fio = new RandomAccessFile(new File(filename), "rw");
	String data = fio.readLine();
	
	System.out.println("Information from file : " + data);
	String content = data;
	System.out.println(content);
	String[] ori = content.split(" ");
	String str = "";
	String temp=ori[0];
	ori[0]=ori[ori.length-1];
	ori[ori.length-1]=temp;  
	
	str = String.join(" ", ori);
	fio.write(str.getBytes(StandardCharsets.UTF_8));
	fio.close();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论