Java 8 使用Java8,我如何读取一个文件的内容,其中我只获得文件中冒号后面的内容?

Java 8 使用Java8,我如何读取一个文件的内容,其中我只获得文件中冒号后面的内容?,java-8,io,Java 8,Io,我的档案如下: Start Simulator Configuration File Version/Phase: 2.0 File Path: Test_2e.mdf Projector cycle time {msec}: 25 Processor cycle time {msec}: 10 Keyboard cycle time {msec}: 50 Monitor display time {msec}: 20 Scanner cycle time {msec}: 10 Hard dri

我的档案如下:

Start Simulator Configuration File
Version/Phase: 2.0
File Path: Test_2e.mdf
Projector cycle time {msec}: 25
Processor cycle time {msec}: 10
Keyboard cycle time {msec}: 50
Monitor display time {msec}: 20
Scanner cycle time {msec}: 10
Hard drive cycle time {msec}: 15
Log: Log to Both
Log File Path: logfile_1.lgf
End Simulator Configuration File
如何将文件读取到只获取“:”之后的内容的位置 我需要十样东西

我设法做到了这一点:

public ConfigureClass(String fileName) throws IOException {


        BufferedReader inputStream = null; 

        try{

            inputStream = new BufferedReader(new FileReader(fileName));

            String test; 
            int index = 0; 

            while((test = inputStream.readLine()) != null){

            arrOfStr[index++] = test.split(": ");


            }
            for(int i =0; i < index; i++){
                System.out.println(arrOfStr[i]);
            }

        } finally{
            if(inputStream != null){
                inputStream.close(); 
            }
        }
        }
    }
公共配置类(字符串文件名)引发IOException{
BufferedReader inputStream=null;
试一试{
inputStream=new BufferedReader(新文件读取器(文件名));
串试验;
int指数=0;
而((test=inputStream.readLine())!=null){
arrOfStr[index++]=test.split(“:”);
}
对于(int i=0;i

我把文件分成几行。每一行都是它自己的字符串。我试着用冒号将这些字符串分割成两个值,一个是前面的值,另一个是后面的值。有了这两个值,我想把它们放在数组中。最好忽略启动模拟器。。。和终端模拟器。。线。我还必须在给定的文件中说明更多的循环时间

这里有一个方法

  • 使用try和资源
  • 检查行是否包含
    并仅拆分为两个字符串,然后选择第二个元素
  • removeIf行为空或null

另一种方法


import java.io.IOException;
导入java.nio.file.Files;
导入java.nio.file.path;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.List;
导入java.util.stream.stream;
公开课考试{
公共静态void main(字符串[]args)引发异常{
System.out.println(Arrays.toString(configureClass(“c:/file.txt”));
}
公共静态字符串[]配置类(字符串文件名)引发IOException{
列表结果=新建ArrayList();
//利用资源进行尝试
try(Stream=Files.line(path.get(fileName))){
流动
.filter(行->行.包含(“:”)
.forEach(line->results.add(line.substring(line.indexOf(“:”)+2,line.length());
}
返回results.toArray(新字符串[0]);
}
}
您可以使用

String[] arrOfStr;
try(Stream<String> lines = Files.lines(Paths.get(fileName))) {
    arrOfStr = lines
            .skip(1).limit(10)
            .map(s -> s.substring(s.indexOf(": ")+2))
            .toArray(String[]::new);
}
Log是对两者的日志
所有值:
处理器周期时间{msec}=10
键盘周期时间{msec}=50
投影仪周期时间{msec}=25
硬盘驱动器周期时间{msec}=15
Log=记录到两个
扫描仪周期时间{msec}=10
监视器显示时间{msec}=20
版本/阶段=2.0
文件路径=Test_2e.mdf
日志文件路径=日志文件_1.lgf
顺便说一句,下次手动处理
BufferedReader
(或任何资源)时,您还应该使用,它可以自动处理关闭(比在
finally
块中手动关闭更好):

字符串头;
地图配置;
try(BufferedReader br=new BufferedReader(new FileReader(fileName))){
header=br.readLine();
config=br.lines()
.map(s->s.split(“:”,2))
.filter(a->a.length==2)
.collect(Collectors.toMap(a->a[0],a->a[1]);
}
System.out.println(“表头为”+表头);
System.out.println(“Log是”+config.getOrDefault(“Log”,“未定义”));
System.out.println(“所有值:”);
config.forEach((key,value)->System.out.println(“+key+”=“+value));
[2.0, Test_2e.mdf, 25, 10, 50, 20, 10, 15, Log to Both, logfile_1.lgf]
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class Test {

    public static void main(String[] args) throws Exception {
        System.out.println(Arrays.toString(configureClass("c:/file.txt")));
    }

    public static String[] configureClass(String fileName) throws IOException {
        List<String> results = new ArrayList<String>();
        // try with resources
        try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
            stream
                .filter(line -> line.contains(": "))
                .forEach(line -> results.add(line.substring(line.indexOf(": ")+2, line.length())));
        }
        return results.toArray(new String[0]);
    }
}
String[] arrOfStr;
try(Stream<String> lines = Files.lines(Paths.get(fileName))) {
    arrOfStr = lines
            .skip(1).limit(10)
            .map(s -> s.substring(s.indexOf(": ")+2))
            .toArray(String[]::new);
}
Map<String,String> config;
try(Stream<String> lines = Files.lines(Paths.get(fileName))) {
    config = lines
            .map(s -> s.split(": ", 2))
            .filter(a -> a.length == 2)
            .collect(Collectors.toMap(a -> a[0], a -> a[1]));
}
System.out.println("Log is "+config.getOrDefault("Log", "Undefined"));
System.out.println("All values:");
config.forEach((key, value) -> System.out.println("  "+key+" = "+value));
String header;
Map<String,String> config;
try(BufferedReader br = new BufferedReader(new FileReader(fileName))) {
    header = br.readLine();
    config = br.lines()
      .map(s -> s.split(": ", 2))
      .filter(a -> a.length == 2)
      .collect(Collectors.toMap(a -> a[0], a -> a[1]));
}
System.out.println("Header is "+header);
System.out.println("Log is "+config.getOrDefault("Log", "Undefined"));
System.out.println("All values:");
config.forEach((key, value) -> System.out.println("  "+key+" = "+value));