我正在尝试将输出写入文件。但是当我编写这段代码时,它会多次写入同一行。如何多次停止重复同一行?
Java_location是我的输出。
这是我的代码:
a = []
a << java_location
File.open("/home/weblogic/javafoundmodified.txt", 'w+') do |file|
a.each { |item| file.puts item }
end
我现在得到的输出是:
1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java
1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java
1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java
1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java
1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java
1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java
1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java
1.7.0_72/u01/java/jdk1.7.0_72/jre/bin/java
1.7.0_72/u01/java/jdk1.7.0_72/jre/bin/java
1.7.0_72/u01/java/jdk1.7.0_72/jre/bin/java
1.7.0_72/u01/java/jdk1.7.0_72/bin/java
1.7.0_72/u01/java/jdk1.7.0_72/bin/java
1.7.0_72/u01/java/jdk1.7.0_72/bin/java
1.7.0_65/u01/java/jdk1.7.0_65/jre/bin/java
1.7.0_65/u01/java/jdk1.7.0_65/jre/bin/java
1.7.0_65/u01/java/jdk1.7.0_65/jre/bin/java
答案 0 :(得分:1)
考虑这一点(javalocation
中的示例数组):
javalocation = %w|
1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java
1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java
1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java
1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java
1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java
1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java
1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java
1.7.0_72/u01/java/jdk1.7.0_72/jre/bin/java
1.7.0_72/u01/java/jdk1.7.0_72/jre/bin/java
1.7.0_72/u01/java/jdk1.7.0_72/jre/bin/java
1.7.0_72/u01/java/jdk1.7.0_72/bin/java
1.7.0_72/u01/java/jdk1.7.0_72/bin/java
1.7.0_72/u01/java/jdk1.7.0_72/bin/java
1.7.0_65/u01/java/jdk1.7.0_65/jre/bin/java
1.7.0_65/u01/java/jdk1.7.0_65/jre/bin/java
1.7.0_65/u01/java/jdk1.7.0_65/jre/bin/java
|
File.open("/home/weblogic/javafoundmodified.txt", 'w+') do |file|
javalocation.uniq.each { |line| file.puts line }
end