从特定行开始读取文件 [英] Begin the reading of a file from a specific line

查看:137
本文介绍了从特定行开始读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于这样的文件:
...

$ p $ 热点服务器JVM具有特定的代码 - 路径优化
#这比客户端版本产生大约10%的增益。
export CATALINA_OPTS =$ CATALINA_OPTS -server
############# HDK1001 #############

#禁用Java客户端的远程(分布式)垃圾收集
#并移除应用程序调用显式GC收集的能力
export CATALINA_OPTS =$ CATALINA_OPTS -XX:+ DisableExplicitGC

#在启动时检查应用程序特定的参数
if [-r$ CATALINA_BASE / bin / appenv.sh];那么
。 $ CATALINA_BASE / bin / appenv.sh
fi
############# HDK7564 #############
#禁用Java客户端的远程(分布式)垃圾收集
#并移除应用程序调用显式GC收集的能力
export CATALINA_OPTS =$ CATALINA_OPTS -XX:+ DisableExplicitGC

我想从存在单词HDK1001的行开始阅读并结束它在世界HDK7564的地方



我尝试了这段代码,但我无法做到这一点

  public static HashMap< String,String> getEnvVariables(String scriptFile,String config){
HashMap< String,String> vars = new HashMap< String,String>();
尝试{

FileInputStream fstream = new FileInputStream(scriptFile);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
字符串strLine;
String var =HDK1001; $($ str

) ($)){
strLine = strLine.substring(7);
扫描仪扫描仪=新扫描仪(strLine);
scanner.useDelimiter(=);
if(scanner.hasNext()){
String name = scanner.next();
字符串值= scanner.next();
System.out.println(name +=+ value);
vars.put(name,value);


$ / code $ / pre
$ b $ p请帮助我


  public static 

HashMap< String,String> getEnvVariables(String scriptFile,
String config){
HashMap< String,String> vars = new HashMap< String,String>();
BufferedReader br = null;
尝试{
FileInputStream fstream = new FileInputStream(scriptFile);
br = new BufferedReader(new InputStreamReader(fstream));
String strLine = null;
String stopvar =HDK7564;
String startvar =HDK1001;
String keyword =export;
如果(strLine!= null&& strLine.contains(startvar)){
if(strLine.contains(stopvar)){
return vars; (strLine!= null&&!strLine.contains(stopvar)){
strLine = br.readLine();

while(strLine!
if(strLine.startsWith(keyword)){
strLine = strLine.substring(keyword.length())
.trim();
String [] split = strLine.split(=);
String name = split [0];
String value = split [1];
System.out.println(name +=+ value);
vars.put(name,value); ((strLine = br.readLine())!= null); $(




} catch(Exception e){
e.printStackTrace();
}
return vars;
}


i have a file similaire to this : ...

The hotspot server JVM has specific code-path optimizations
# which yield an approximate 10% gain over the client version.
export CATALINA_OPTS="$CATALINA_OPTS -server"
#############HDK1001#############

# Disable remote (distributed) garbage collection by Java clients
# and remove ability for applications to call explicit GC collection
export CATALINA_OPTS="$CATALINA_OPTS -XX:+DisableExplicitGC"

# Check for application specific parameters at startup
if [ -r "$CATALINA_BASE/bin/appenv.sh" ]; then
. "$CATALINA_BASE/bin/appenv.sh"
fi
 #############HDK7564#############
# Disable remote (distributed) garbage collection by Java clients
# and remove ability for applications to call explicit GC collection
export CATALINA_OPTS="$CATALINA_OPTS -XX:+DisableExplicitGC"

i want to begin the reading from the line where exists the word "HDK1001" and end it where the world "HDK7564"

i tryed with this code but i am unable to do the limitation

public static HashMap<String, String> getEnvVariables(String scriptFile,String config) {
    HashMap<String, String> vars = new HashMap<String, String>();
    try {

        FileInputStream fstream = new FileInputStream(scriptFile);
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
        String strLine;
        String var= "HDK1001";

        while ((strLine = br.readLine()) != null  ) {

            if (strLine.startsWith("export") && !strLine.contains("$")) {
                strLine = strLine.substring(7);
                Scanner scanner = new Scanner(strLine);
                scanner.useDelimiter("=");
                if (scanner.hasNext()) {
                    String name = scanner.next();
                    String value = scanner.next();
                    System.out.println(name+"="+value);
                    vars.put(name, value);
                }
            }

Help me please

解决方案

try this code.

public static HashMap<String, String> getEnvVariables(String scriptFile,
            String config) {
        HashMap<String, String> vars = new HashMap<String, String>();
        BufferedReader br = null;
        try {
            FileInputStream fstream = new FileInputStream(scriptFile);
            br = new BufferedReader(new InputStreamReader(fstream));
            String strLine = null;
            String stopvar = "HDK7564";
            String startvar = "HDK1001";
            String keyword = "export";
            do {
                if (strLine != null && strLine.contains(startvar)) {
                    if (strLine.contains(stopvar)) {
                        return vars;
                    }
                    while (strLine != null && !strLine.contains(stopvar)) {
                        strLine = br.readLine();
                        if (strLine.startsWith(keyword)) {
                            strLine = strLine.substring(keyword.length())
                                    .trim();
                            String[] split = strLine.split("=");
                            String name = split[0];
                            String value = split[1];
                            System.out.println(name + "=" + value);
                            vars.put(name, value);
                        }
                    }
                }
            } while ((strLine = br.readLine()) != null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vars;
    }

这篇关于从特定行开始读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆