file获取文件内容|php如何获取文件内容

file获取文件内容|php如何获取文件内容的第1张示图

⑴ java编程:从一个名为file的文件中逐行读取然后将读取的内容放进另一个文件file1中。

/** * 以行为单位读取文件,常用于读面向行的格式化文件 * * @param fileName * 文件名 */public static void readFileByLines(String fileName) { file = new File(fileName);BufferedReader reader = null;try {System.out.println("以行为单位读取文件内容,一次读一整行:");reader = new BufferedReader(new FileReader(file));String tempString = null;int line = 1;// 一次读入一行,直到读入null为文件结束while ((tempString = reader.readLine()) != null) {// 显示行号System.out.println("line " + line + ": " + tempString);line++;}reader.close();} catch (IOException e) {e.printStackTrace();} finally {if (reader != null) {try {reader.close();} catch (IOException e1) {}}}} /** * 文件写入 * * @param filePath 路径名称 * @param sb 要写入的字符 */public void writeFromBuffer(String filePath, String sb)throws IOException {File file = new File(filePath);FileWriter fw;try {fw = new FileWriter(file);if (sb.toString() != null && !"".equals(sb.toString())) {fw.write(sb.toString());}fw.close();} catch (IOException e) {throw new IOException("文件写入异常!请检查路径名是否正确!");}}自己组织一下,读取的数据可以放在stringbuffer里然后在传给写入方法

⑵ Java如何获取文件的内容类型

如果是要获取文件的类型格式的,先取得文件的名字,然后通过字符串截取(从最后一一个点开始截取)。File file =new File("");String fileName=File.getName();fileName.subString(fileName.lastIndexOf("."));

⑶ C读取文件内容

C读取文件内容参考代码如下:

#include<stdio.h>#include<stdlib.h>#include<string.h>#defineMAX_LINE1024intmain(){charbuf[MAX_LINE];/*缓冲区*/FILE*fp;/*文件指针*/intlen;/*行字符个版数*/if((fp=fopen("test.txt","r"))==NULL){perror("failtoread");exit(1);}while(fgets(buf,MAX_LINE,fp)!权=NULL){len=strlen(buf);buf[len-1]='';/*去掉换行符*/printf("%s%d",buf,len-1);}return0;}

⑷ php如何获取文件内容

PHP 中的file_get_contents() 函数可以实现file_get_contents() 函数把整个文件读入一个字符串中。和 file() 一样,版不同的是 file_get_contents() 把文件读入一个字符串。file_get_contents() 函数是权用于将文件的内容读入到一个字符串中的首选方法。如果操作系统支持,还会使用内存映射技术来增强性能。例如:<?phpecho file_get_contents("test.txt");?>

⑸ java如何读取txt文件内容

给你两个方法,你可以看看;//获取值返回String文本publicStringtxt2String(StringfilePath){Filefile=newFile(filePath);StringBuilderresult=newStringBuilder();try{BufferedReaderbr=newBufferedReader(newFileReader(file));//构造一个BufferedReader类来读取文件Strings=null;while((s=br.readLine())!=null){//使用readLine方法,一次读一行result.append(s+System.lineSeparator());}br.close();}catch(Exceptione){e.printStackTrace();}returnresult.toString();}//获取值不返回String文本publicvoidreadTxtFile(StringfilePath){try{Stringencoding="GBK";Filefile=newFile(filePath);if(file.isFile()&&file.exists()){//判断文件是否存在InputStreamReaderread=newInputStreamReader(newFileInputStream(file),encoding);//考虑到编码格式BufferedReaderbufferedReader=newBufferedReader(read);StringlineTxt=null;while((lineTxt=bufferedReader.readLine())!=null){System.out.println(lineTxt);}read.close();}else{System.out.println("找不到指定的文件");}}catch(Exceptione){System.out.println("读取文件内容出错");e.printStackTrace();}}

⑹ 如何:读取文本文件中的内容

在C语言中,文件操作都是由库函数来完成的。要读取一个txt文件,首先要使用文件打开函数fopen()。fopen函数用来打开一个文件,其调用的一般形式为: 文件指针名=fopen(文件名,使用文件方式) 其中,“文件指针名”必须是被说明为FILE 类型的指针变量,“文件名”是被打开文件的文件名。 “使用文件方式”是指文件的类型和操作要求。“文件名”是字符串常量或字符串数组。其次,使用文件读写函数读取文件。在C语言中提供了多种文件读写的函数: ·字符读写函数 :fgetc和fputc·字符串读写函数:fgets和fputs·数据块读写函数:freed和fwrite·格式化读写函数:fscanf和fprinf最后,在文件读取结束要使用文件关闭函数fclose()关闭文件。下面以格式化读写函数fscanf和fprintf为例,实现对文件A.txt(各项信息以空格分割)的读取,并将它的信息以新的格式(用制表符分割各项信息)写入B.txt,实现对A.txt的处理。

⑺ java上传txt文件后读取内容

告诉你很麻烦FormFile upfile = fileupForm.getFilecontext();// 获取客户端选中的文件实体FileReader fr = new FileReader(upfile); BufferedReader br = new BufferedReader(fr); String line=""; int lineNum=0; while((line=br.readLine())!=null){ System.out.println(line); lineNum++; if(lineNum==N) break; //当读取的行数专为自定义的属行数N时break.退出 } br.close(); fr.close();

⑻ input file 在js里获取文件内容

javascript 获取文件域 (type=file) 的完整路径一直是很麻烦的问题,问题主要出在一些浏览器基于安全性考虑而不能正常获取到文件域中选中图片的决对路径,尤其一些基于webkit的浏览器比如 Chrome, Safire等浏览器,下面是一个可以兼容 IE 6 , 7, 8 和 firefox 的获取 input file 完整路径的方法,该方法不支持 Chrome 和 Safire,要支持这些浏览器可能要使用到 Flash ,对程序员来说比较麻烦。

HTML页面:<inputtype="file"name="attachment"id="attachment"/><inputtype="text"name="trueattachment"id="trueattachment"/>js部分:<scripttype="text/javascript">varattachment=document.getElementById("attachment");vartrueattachment=document.getElementById("trueattachment");attachment.onchange=function(){trueattachment.value=getFullPath(this);}functiongetFullPath(obj){if(obj){//ieif(window.navigator.userAgent.indexOf("MSIE")>=1){obj.select();returndocument.selection.createRange().text;}//firefoxelseif(window.navigator.userAgent.indexOf("Firefox")>=1){if(obj.files){returnobj.files.item(0).getAsDataURL();}returnobj.value;}returnobj.value;}}</script>

⑼ 怎么从文件中读取文件中的一部分内容

先将文件全部读入 char* 变量。再用 string 类 构建函数建一个string 对象,在把 char* 内容放入。下面是将文件全部读入char * buffer;/* fread example: read an entire file */ #include <stdio.h> #include <stdlib.h> int main () { FILE * pFile; long lSize; char * buffer; size_t result; pFile = fopen ( "myfile.bin" , "rb" ); if (pFile==NULL) {fputs ("File error",stderr); exit (1);} // obtain file size: fseek (pFile , 0 , SEEK_END); lSize = ftell (pFile); rewind (pFile); // allocate memory to contain the whole file: buffer = (char*) malloc (sizeof(char)*lSize); if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);} // the file into the buffer: result = fread (buffer,1,lSize,pFile); if (result != lSize) {fputs ("Reading error",stderr); exit (3);} /* the whole file is now loaded in the memory buffer. */ // terminate fclose (pFile); free (buffer); return 0; }构建函数建一个string 对象,把 char * buffer 内容存入 程序部分,请自己补充:#include <windows.h>#include<iostream>#include <string>using namespace std;#include <stdio.h>// 插入上面程序 …..// 补充string sss;sss.assign(buffer,result);cout << sss << endl;

未经允许不得转载:山九号 » file获取文件内容|php如何获取文件内容

赞 (0)