📄 readfile.jsp
字号:
<%/***********//* Author : JarryLi /* Blog:http://jiarry.blogchina.com/***********/%><%@ page import="java.util.*"%><%@ page import="java.lang.*" %><%@ page import="javax.servlet.*" %><%@ page import="javax.servlet.jsp.*" %><%@ page import="javax.servlet.http.* "%><%@ page import="java.io.*" %><%@ page import="java.nio.*" %><%@ page import="java.nio.channels.*" %><%@ page import="java.lang.Object.*" %><%! //返回得到的文件内容,如果文件编码是UTF-8 //根据传入的encoding把UTF-8转码成GBK;public String readContent(String fn , String encoding ){ //return the file all contents; String content=""; String inLine=null; if(encoding == null || encoding.length() <=0 )encoding = "GBK"; //默认从UTF-8转为GB2312; try{ //创建新的BufferedReader对象 BufferedReader readfile = new BufferedReader( new FileReader( fn ) ); inLine = readfile.readLine(); while ( inLine != null) { //content += inLine; content += (inLine + System.getProperty("line.separator")); inLine = readfile.readLine(); } content = new String(content.getBytes(encoding),"UTF-8") ; }catch (IOException e){ //out.print("读取数据错误."); } return content;}public String readContents(String fn){ //返回得到的文件 //return the file all contents; String content=""; String inLine=null; try{ // ByteArrayOutputStream out1 = new ByteArrayOutputStream(); // FileInputStream fin = new FileInputStream(filename); // BufferedReader in = new BufferedReader(new InputStreamReader(fin,"UTF-8")); //这里指定编码 //创建新的BufferedReader对象 BufferedReader readfile = new BufferedReader( new FileReader( fn ) ); inLine = readfile.readLine(); while ( inLine != null) { //content += inLine; content += (inLine + System.getProperty("line.separator")); inLine = readfile.readLine(); } }catch (IOException e){ } return content;}//return the line count function;private int lineCount( String thefile){ //thefile is a text file; //count the file's rows; int rowCount = 0; String rowline = null; try{ BufferedReader readfn = new BufferedReader( new FileReader(thefile) ); while( (rowline = readfn.readLine()) != null ){ rowCount++; } }catch(IOException e){ //out.print("异常:"+e.getMessage()) } return rowCount;}//return success or failed;private String writeContent( String content , String fn){ //write content in fn; //直接写入,不做排他; try{ PrintWriter pw = new PrintWriter( new FileOutputStream(fn) );//声明写入 pw.print(content); pw.close();//close PrintWriter对象 return "success";//writeln successfull; }catch(IOException e) { return "failed"; } /* //写入文件时锁定,以避免两个同时写入的操作情况; try { File file = new File(fn); //FileChannel channel = new RandomAccessFile(file, "rw").getChannel(); FileLock lock = channel.lock(); try { // lock = channel.tryLock();//锁定; PrintWriter pw = new PrintWriter( new FileOutputStream(fn) );//声明写入 pw.print(content); pw.close();//close PrintWriter对象 return "success";//writeln successfull; }catch (OverlappingFileLockException e) { } lock.release(); // Close the file channel.close(); } catch (Exception e) { } return "failed"; */}//转义js字符private String turnScript(String temStr){ try{ temStr = temStr.replaceAll("\\\\","\\\\\\\\"); // 转换\ temStr = temStr.replaceAll("\"","\\\\\""); // 转换" temStr = temStr.replaceAll("'","\\\\'"); // 转换' temStr = temStr.replaceAll("\r","<br/>"); // 转换回车 temStr = temStr.replaceAll("\n","<br/>"); // 转换换行符 }catch(Exception e) { //return "failed"; } return temStr;}//返回文件里指定的某行内容;private String readContentLine(String fn , int no){ String content=""; String inLine=null; int rowCount = 0; try{ //创建新的BufferedReader对象 BufferedReader readfile = new BufferedReader( new FileReader( fn ) ); inLine = readfile.readLine(); while ( inLine != null) { rowCount ++; //content += inLine; if(rowCount == no){ content = (inLine + System.getProperty("line.separator")); } inLine = readfile.readLine(); } }catch (IOException e){ //out.print("读取数据错误."); } return content;}//返回文件除某行之外的内容;private String readContentNoTheLine(String fn , int no){ String content=""; String inLine=null; int rowCount = 0; try{ //创建新的BufferedReader对象 BufferedReader readfile = new BufferedReader( new FileReader( fn ) ); while ( (inLine = readfile.readLine()) != null) { rowCount ++; if(rowCount != no){ content += (inLine + System.getProperty("line.separator")); } } }catch (IOException e){ //out.print("读取数据错误."); } return content;}//返回rowNum 是否与arraylist数组里的某个值相同;private boolean isDeleteRow(String[] arraylist , int rowNum){ //如果提定的行号与数组里的任何一个行号相同返回true; //说明是要要删除的行号; for(int x=0; x< arraylist.length;x++){ if( rowNum == Integer.parseInt(arraylist[x]) ) return true;//比较当前行号与数据里的行号 } return false;}//return true or false,//check the string is not number;public boolean isNum(String str){ if(str.length() <= 0 )return false; boolean num = true; for(int i=0; i< str.length();i++){ if( str.charAt(i) < '0' || str.charAt(i) > '9'){ num = false; } } return num;}//如果后台操作程序不是采用utf-8编码,如GBK;采用utf-8则不用转码,支持utf-8哦//form GET方式提交过来的字符串是unicode编码;//那么读取中文字符时需要转码成GBK;//return Chinese charpublic String getStrCn(String str){ try{ String temp_p=str; byte[] temp_t=temp_p.getBytes("ISO-8859-1"); String temp=new String(temp_t,"GBK"); return temp; }catch(Exception e){ } return "null";}//如果后台操作程序不是采用utf-8编码,如GBK;采用utf-8则不用转码,支持utf-8哦//js提交(xmlhttp GET)过来的encodeURIComponent(String)字符串是utf-8编码;//那么传递中文字符时需要转码成utf-8保存;public String decodeUTF(String str){ try{ String temp_p = java.net.URLDecoder.decode(str); String temp = new String(temp_p.getBytes("ISO-8859-1"),"UTF-8"); return temp; }catch(Exception e){ } return "null";}//decodeGBKpublic String decodeGBK(String str){ try{ String temp_p = java.net.URLDecoder.decode(str); String temp = new String(temp_p.getBytes("ISO-8859-1"),"GBK"); return temp; }catch(Exception e){ } return "null";}//如果后台不是UTF-8编码,比如是GBK;//如果采用xmlhttp POST方式传值,需要把传来的值从GBK转换成UTF-8;public String GBKtoUTF(String str){ try{ String temp = new String(str.getBytes("GBK"),"UTF-8"); return temp; }catch(Exception e){ } return "null";}//如果后台不是UTF-8编码,比如是GBK;//如果采用xmlhttp POST方式传值,需要把传来的值从UTF-8转换成GBK;public String UTFtoGBK(String str){ try{ String temp = new String(str.getBytes("UTF-8"),"GBK"); return temp; }catch(Exception e){ } return "null";}//本函数可以写在Bean里,本例为了说明Ajax的应用,故直接写在页面//return file contents function;// jsp:include page="../inc/readfile.jsp" /// %@ include file="../inc/readfile.jsp" %%>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -