}' `profile是你的配置文件名,这个是把所有内容存到con中。如果要加分隔符号的话,在printf中加就行,不过要注意用双引号引起来package "og:type" content="webpage">}' `profile是你的配置文件名,这个是把所有内容存到con中。如果要加分隔符号的话,在printf中加就行,不过要注意用双引号引起来package " /> }' `profile是你的配置文件名,这个是把所有内容存到con中。如果要加分隔符号的话,在printf中加就行,不过要注意用双引号引起来package "> linux shell 读取一个配置文件并获取其中的全部内容,急!!!!!!!_框架_内存溢出

阅读19

linux shell 读取一个配置文件并获取其中的全部内容,急!!!!!!!,第1张

con=`cat profile|awk '{printf $0}' `profile是你的配置文件名,这个是把所有内容

存到con中。如果要加分隔符号的话,在printf中加就行,不过要注意用双引号引起来

package comlwjdemo;  

  

import javaio; 

public class RandomAccessFileDemo {  

 public static void main(String[] args) throws Exception {  

    RandomAccessFile file = new RandomAccessFile("file", "rw");  

      // 以下向file文件中写数据    filewriteInt(20);// 占4个字节

  

  filewriteDouble(8236598);// 占8个字节  

  filewriteUTF("这是一个UTF字符串");// 这个长度写在当前文件指针的前两个字节处,可用readShort()读取  

  filewriteBoolean(true);// 占1个字节  

  filewriteShort(395);// 占2个字节  

  filewriteLong(2325451l);// 占8个字节  

  filewriteUTF("又是一个UTF字符串");  

  filewriteFloat(355f);// 占4个字节  

  filewriteChar('a');// 占2个字节  

  

  fileseek(0);// 把文件指针位置设置到文件起始处  

  

  // 以下从file文件中读数据,要注意文件指针的位置  

  Systemoutprintln("——————从file文件指定位置读数据——————");  

  Systemoutprintln(filereadInt());  

  Systemoutprintln(filereadDouble());  

  Systemoutprintln(filereadUTF());  

  

  fileskipBytes(3);// 将文件指针跳过3个字节,本例中即跳过了一个boolean值和short值。  

  Systemoutprintln(filereadLong());  

  

  fileskipBytes(filereadShort()); // 跳过文件中“又是一个UTF字符串”所占字节,注意readShort()方法会移动文件指针,所以不用加2。  

  Systemoutprintln(filereadFloat());  

   }

}

参考资料

>

  Java读取目录下的文件内容,使用的是java的文件类,示例如下:

import javaioBufferedReader;

import javaioFile;

import javaioFileInputStream;

import javaioFileReader;

import javaioIOException;

import javaioInputStream;

import javaioInputStreamReader;

import javaioRandomAccessFile;

import javaioReader;

 

public class ReadFromFile {

    /

      以字节为单位读取文件,常用于读二进制文件,如、声音、影像等文件。

      

      @param fileName

                 文件的名

     /

    public static void readFileByBytes(String fileName) {

        File file = new File(fileName);

        InputStream in = null;

        try {

            Systemoutprintln("以字节为单位读取文件内容,一次读一个字节:");

            // 一次读一个字节

            in = new FileInputStream(file);

            int tempbyte;

            while ((tempbyte = inread()) != -1) {

                Systemoutwrite(tempbyte);

            }

            inclose();

        } catch (IOException e) {

            eprintStackTrace();

            return;

        }

        try {

            Systemoutprintln("以字节为单位读取文件内容,一次读多个字节:");

            // 一次读多个字节

            byte[] tempbytes = new byte[100];

            int byteread = 0;

            in = new FileInputStream(fileName);

            ReadFromFileshowAvailableBytes(in);

            // 读入多个字节到字节数组中,byteread为一次读入的字节数

            while ((byteread = inread(tempbytes)) != -1) {

                Systemoutwrite(tempbytes, 0, byteread);

            }

        } catch (Exception e1) {

            e1printStackTrace();

        } finally {

            if (in != null) {

                try {

                    inclose();

                } catch (IOException e1) {

                }

            }

        }

    }

 

    /

      以字符为单位读取文件,常用于读文本,数字等类型的文件

      

      @param fileName

                 文件名

     /

    public static void readFileByChars(String fileName) {

        File file = new File(fileName);

        Reader reader = null;

        try {

            Systemoutprintln("以字符为单位读取文件内容,一次读一个字节:");

            // 一次读一个字符

            reader = new InputStreamReader(new FileInputStream(file));

            int tempchar;

            while ((tempchar = readerread()) != -1) {

                // 对于windows下,\r\n这两个字符在一起时,表示一个换行。

                // 但如果这两个字符分开显示时,会换两次行。

                // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。

                if (((char) tempchar) != '\r') {

                    Systemoutprint((char) tempchar);

                }

            }

            readerclose();

        } catch (Exception e) {

            eprintStackTrace();

        }

        try {

            Systemoutprintln("以字符为单位读取文件内容,一次读多个字节:");

            // 一次读多个字符

            char[] tempchars = new char[30];

            int charread = 0;

            reader = new InputStreamReader(new FileInputStream(fileName));

            // 读入多个字符到字符数组中,charread为一次读取字符数

            while ((charread = readerread(tempchars)) != -1) {

                // 同样屏蔽掉\r不显示

                if ((charread == tempcharslength)

                        && (tempchars[tempcharslength - 1] != '\r')) {

                    Systemoutprint(tempchars);

                } else {

                    for (int i = 0; i < charread; i++) {

                        if (tempchars[i] == '\r') {

                            continue;

                        } else {

                            Systemoutprint(tempchars[i]);

                        }

                    }

                }

            }

 

        } catch (Exception e1) {

            e1printStackTrace();

        } finally {

            if (reader != null) {

                try {

                    readerclose();

                } catch (IOException e1) {

                }

            }

        }

    }

 

    /

      以行为单位读取文件,常用于读面向行的格式化文件

      

      @param fileName

                 文件名

     /

    public static void readFileByLines(String fileName) {

        File file = new File(fileName);

        BufferedReader reader = null;

        try {

            Systemoutprintln("以行为单位读取文件内容,一次读一整行:");

            reader = new BufferedReader(new FileReader(file));

            String tempString = null;

            int line = 1;

            // 一次读入一行,直到读入null为文件结束

            while ((tempString = readerreadLine()) != null) {

                // 显示行号

                Systemoutprintln("line " + line + ": " + tempString);

                line++;

            }

            readerclose();

        } catch (IOException e) {

            eprintStackTrace();

        } finally {

            if (reader != null) {

                try {

                    readerclose();

                } catch (IOException e1) {

                }

            }

        }

    }

 

    /

      随机读取文件内容

      

      @param fileName

                 文件名

     /

    public static void readFileByRandomAccess(String fileName) {

        RandomAccessFile randomFile = null;

        try {

            Systemoutprintln("随机读取一段文件内容:");

            // 打开一个随机访问文件流,按只读方式

            randomFile = new RandomAccessFile(fileName, "r");

            // 文件长度,字节数

            long fileLength = randomFilelength();

            // 读文件的起始位置

            int beginIndex = (fileLength > 4)  4 : 0;

            // 将读文件的开始位置移到beginIndex位置。

            randomFileseek(beginIndex);

            byte[] bytes = new byte[10];

            int byteread = 0;

            // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。

            // 将一次读取的字节数赋给byteread

            while ((byteread = randomFileread(bytes)) != -1) {

                Systemoutwrite(bytes, 0, byteread);

            }

        } catch (IOException e) {

            eprintStackTrace();

        } finally {

            if (randomFile != null) {

                try {

                    randomFileclose();

                } catch (IOException e1) {

                }

            }

        }

    }

 

    /

      显示输入流中还剩的字节数

      

      @param in

     /

    private static void showAvailableBytes(InputStream in) {

        try {

            Systemoutprintln("当前字节输入流中的字节数为:" + inavailable());

        } catch (IOException e) {

            eprintStackTrace();

        }

    }

 

    public static void main(String[] args) {

        String fileName = "C:/temp/newTemptxt";

        ReadFromFilereadFileByBytes(fileName);

        ReadFromFilereadFileByChars(fileName);

        ReadFromFilereadFileByLines(fileName);

        ReadFromFilereadFileByRandomAccess(fileName);

    }

}

java读取txt文件内容。可以作如下理解:

首先获得一个文件句柄。File file = new File(); file即为文件句柄。两人之间连通电话网络了。接下来可以开始打电话了。

通过这条线路读取甲方的信息:new FileInputStream(file) 目前这个信息已经读进来内存当中了。接下来需要解读成乙方可以理解的东西

既然你使用了FileInputStream()。那么对应的需要使用InputStreamReader()这个方法进行解读刚才装进来内存当中的数据

解读完成后要输出呀。那当然要转换成IO可以识别的数据呀。那就需要调用字节码读取的方法BufferedReader()。同时使用bufferedReader()的readline()方法读取txt文件中的每一行数据哈。

package comcampu;

 

import javaioBufferedInputStream;

import javaioBufferedReader;

import javaioFile;

import javaioFileInputStream;

import javaioInputStreamReader;

import javaioReader;

 

/

  @author Java团长

  H20121012java

  2017-10-29上午11:22:21

 /

public class H20121012 {

    /

      功能:Java读取txt文件的内容

      步骤:1:先获得文件句柄

      2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取

      3:读取到输入流后,需要读取生成字节流

      4:一行一行的输出。readline()。

      备注:需要考虑的是异常情况

      @param filePath

     /

    public static void readTxtFile(String filePath){

        try {

                String encoding="GBK";

                File file=new File(filePath);

                if(fileisFile() && fileexists()){ //判断文件是否存在

                    InputStreamReader read = new InputStreamReader(

                    new FileInputStream(file),encoding);//考虑到编码格式

                    BufferedReader bufferedReader = new BufferedReader(read);

                    String lineTxt = null;

                    while((lineTxt = bufferedReaderreadLine()) != null){

                        Systemoutprintln(lineTxt);

                    }

                    readclose();

        }else{

            Systemoutprintln("找不到指定的文件");

        }

        } catch (Exception e) {

            Systemoutprintln("读取文件内容出错");

            eprintStackTrace();

        }

     

    }

     

    public static void main(String argv[]){

        String filePath = "L:\\Apache\\htdocs\\res\\20121012txt";

//      "res/";

        readTxtFile(filePath);

    }

     

     

 

}我有一个微信公众号,经常会分享一些Java技术相关的干货文章,还有一些学习资源。

如果你需要的话,可以用微信搜索“Java团长”或者“javatuanzhang”关注。

作为C++风格的文件读取方式

可以使用文件流类——fstream类

fstream类有两种子类

分别是用于读出文件的ifstream类

以及用于写入文件ofstream类

在使用是应加入引用 : #include <fstream>

注意该头文件使用std命名空间

还应该加入语句 :using namespace std;

使用的使用应该创建一个文件流对象

比如读入一个文件可以使用下列语句:

ifstream File;

char FileName;

char DataBuffer[128];

/ 此处应设定文件名 /

Fileopen(FileName); //打开文件

//open函数其实有三个参数,此处后两个使用默认值了,具体函数使用请见MSDN

if(File)

{ //文件打开成功

// 此处加入对文件内容的处理

while(!Fileeof())

{ //循环读入数据

Fileread(DataBuffer,128);

/对缓冲区中的读入数据进行 *** 作/

}

}

else

{ //文件打开失败

/进行错误处理/

}

Fileclose(); //关闭文件

与上述代码类似

将内容写入文件需要创建一个ofstream对象

可以多看看MDSN

可以参考CPP标准函数库

从文件内容读入和输出可以使用fscanf、fprintf。

如:

fscanf(fp, "%d", &num); //从文件fp读入一个整数到num变量;

fprintf(fp, "%d", num); //将num变量的值输出到文件fp;

屏幕也可以看作一个文件流,输入是stdin,输出是stdout,如输出到屏幕,就是:

fprintf(stdout, "%d", num);

使用fread函数读取指定长度的字符串,即使包含\n也会被读取,可以首先使用fseek定位到文件结尾,然后ftell函数返回的值就是文件的大小,这样就可以用循环多次读取文件,直到读取所有内容

FILE file = NULL;

char szFile[1025] = {0};

int nHadRead = 0;

file = fopen( "filetxt", "r+");

if ( file == NULL )

return;

fseek( file, 0, SEEK_END ); //定位到文件尾

int nLen = ftell( file ); //获取当前位置,即文件长度

fseek( file 0, SEEK_SET ); //重新定位到文件开头,准备开始读

while ( nHadRead < nLen )

{

int nRead = nLen - nHadRead >1024 1024 : nLen - nHadRead; //如果剩余小于1024字节,则读剩余字节,否则每次读取1024字节。

int nTmp = fread( szFile, 1, nRead , file );

nHadRead += nTmp;

printf( "%s", szFile );

memset( szFile, 0x0, sizeof(szFile) );

}

fclose(file);

大致过程就是这样,纯手打,没有调试过,可能有错

以上就是关于linux shell 读取一个配置文件并获取其中的全部内容,急!!!!!!!全部的内容,包括:linux shell 读取一个配置文件并获取其中的全部内容,急!!!!!!!、java中如何读取文件中特定行的内容、Java 如何读取目录下的文件内容等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:

内存溢出

原文地址:

https://54852.com/web/9633657.html
多个
(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
陈彦铭陈彦铭一级用户组
MYsql添加一条新数据获取添加的ID,ASP.NET里怎么做求代码
上一篇
2023-04-30
SpringBoot 整合线程池及各参数详解
2023-04-30

发表评论
请登录后评论...
登录
提交

    评论列表(0条)
保存
{label}{label}}' `profile是你的配置文件名,这个是把所有内容存到con中。如果要加分隔符号的话,在printf中加就行,不过要注意用双引号引起来package ', author : '陈彦铭', cat_name : '框架', time_y_m : '2023年04月', time_d : '30', site_motto : '内存溢出' }; {script}{script}