如何在Java中交换文件中的第一个单词和最后一个单词?

huangapple go评论73阅读模式
英文:

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(&quot;Enter file name : &quot;);
        filename = reader.readLine();
        fio = new RandomAccessFile(new File(filename), &quot;r&quot;);
        data = fio.readLine();
        System.out.println(&quot;Information from file : &quot; + data);
        String content = fio.toString();
        System.out.println(content);
        String[] ori = content.split(&quot; &quot;);
        String str = &quot;&quot;;
        String temp=ori[0];
        ori[0]=ori[ori.length-1];
        ori[ori.length-1]=temp;   

        for(int i = 1; i &lt; 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(&quot;Enter file name : &quot;);
	String filename = reader.readLine();
	RandomAccessFile fio = new RandomAccessFile(new File(filename), &quot;rw&quot;);
	String data = fio.readLine();
	
	System.out.println(&quot;Information from file : &quot; + data);
	String content = data;
	System.out.println(content);
	String[] ori = content.split(&quot; &quot;);
	String str = &quot;&quot;;
	String temp=ori[0];
	ori[0]=ori[ori.length-1];
	ori[ori.length-1]=temp;  
	
	str = String.join(&quot; &quot;, ori);

	fio.write(str.getBytes(StandardCharsets.UTF_8));
	fio.close();
}

huangapple
  • 本文由 发表于 2020年9月30日 04:01:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64126817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定