Java读取.wps后缀名文档的代码?
可以通过流的方式加载.wps文档,下面以读取文档中的文字保存到本地为例,你参考看看如何读取的。
import com.spire.doc.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
public class ReadTextFromWPS {
public static void main(String[] args) throws IOException{
//通过流加载WPS文字文档
FileInputStream inputStream = new FileInputStream(new File(“test.wps”));
Document doc = new Document();
doc.loadFromStream(inputStream, FileFormat.Doc);
//获取文本保存为String
String text = doc.getText();
//将String写入Txt
writeStringToTxt(text,”读取WPS文本.txt”);
}
public static void writeStringToTxt(String content, String txtFileName) throws IOException {
FileWriter fWriter= new FileWriter(txtFileName,true);
try {
fWriter.write(content);
}catch(IOException ex){
ex.printStackTrace();
}finally{
try{
fWriter.flush();
fWriter.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
读取结果:
注意在程序中导入spire.doc.jar。
java对文件名的几个操作,获取文件扩展名,去掉扩展名
/*
* Java文件操作 获取文件扩展名
*
* Created on: 2011-8-2
* Author: blueeagle
*/
public static String getExtensionName(String filename) {
if ((filename != null) (filename.length() 0)) {
int dot = filename.lastIndexOf(‘.’);
if ((dot -1) (dot (filename.length() – 1))) {
return filename.substring(dot + 1);
}
}
return filename;
}
/*
* Java文件操作 获取不带扩展名的文件名
*
* Created on: 2011-8-2
* Author: blueeagle
*/
public static String getFileNameNoEx(String filename) {
if ((filename != null) (filename.length() 0)) {
int dot = filename.lastIndexOf(‘.’);
if ((dot -1) (dot (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename;
}
Java Base64 直接获取文件后缀
import sun.misc.BASE64Encoder; import sun.misc.BASE64Decoder; // 将 s 进行 BASE64 编码 public static String getBASE64(String s) { if (s == null) return null; return (new sun.misc.BASE64Encoder()).encode( s.getBytes() ); }敞饥搬渴植韭邦血鲍摩 // 将 BASE64 编码的字符串 s 进行解码 public static String getFromBASE64(String s) { if (s == null) return null; BASE64Decoder decoder = new BASE64Decoder(); try { byte[] b = decoder.decodeBuffer(s); return new String(b); } catch (Exception e) { return null; } }
jsp或者JAVA如何获取文件的后缀名字吗?
public String getFileType(String fileUri){
File file = new File(fileUri);
String fineName = file.getName();
String fileType = fileName.substring(fileName.lastIndexOf(\”.\”)+1,fileName.length())
return fileType;
}
java怎么获取上传文件的后缀
给你个示例,应该看得懂吧
File f =new File(“Test.txt”);
String fileName=f.getName();
String prefix=fileName.substring(fileName.lastIndexOf(“.”)+1);
System.out.println(prefix);
}