package comylqtest;
import javaioBufferedReader;
import javaioBufferedWriter;
import javaioFile;
import javaioFileReader;
import javaioFileWriter;
import javaioIOException;
import javautilArrayList;
import javautilHashMap;
import javautilList;
public class Test {
NumSaver saver = new NumSaver();
public static void main(String[] args) throws IOException {
Test test = new Test();
testread("data/filetxt","data/outtxt");
}
public void read(String pathIn, String pathOut) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(new File(pathIn)));
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(pathOut)));
String tmp = null;
while((tmp = readerreadLine()) != null){
Systemoutprintln(tmp);
String years = getYearRange(tmp);
writerwrite(yearssubstring(1,yearslength() - 1) + "\n");
}
writerwrite(savertoString());
writerflush();
readerclose();
writerclose();
}
public String getYearRange(String tmp){
String[] range = tmpsplit(" \\- ");
List<Integer> years = new ArrayList<>();
for(int i = IntegervalueOf(range[0]); i <= IntegervalueOf(range[1]); i++){
yearsadd(i);
saversave(i);
}
return yearstoString();
}
public class NumSaver extends HashMap<Integer, Integer>{
private static final long serialVersionUID = 8101506360053704633L;
public void save(int num){
thisput(num, null);
}
@Override
public Integer put(Integer key, Integer value) {
return superput(key, superget(key) == null 1 : superget(key) + 1);
}
@Override
public String toString() {
StringBuilder strBuilder = new StringBuilder();
for(Entry<Integer, Integer> entry : thisentrySet()){
strBuilderappend(entrygetKey() + ":" + entrygetValue() + ", ");
}
return strBuildertoString()substring(0,strBuilderlastIndexOf(","));
}
}
}
目录结构如下图所示:
看看是否满足你的要求
给你看一个小程序就懂了。
private static String getSubStr(String str, int num) {
String result = "";
int i = 0;
while(i < num) {
int lastFirst = strlastIndexOf('/');
result = strsubstring(lastFirst) + result;
str = strsubstring(0, lastFirst);
i++;
}
return resultsubstring(1);
} }
想去倒数第几个后面的字符串就把num设成几,str是原来的字符串,返回得到字符串
if (!FileUpload1Exists(savePath))
{
ResponseWrite("对不起!文件不存在!!");
return;
}
这里的FileUpload1,是不是应该写成FileExists,FileUpload1看上去像是控件名称,而对服务器上的文件检查应用的File类。请核实。而FileUpload控件只是用来上传文件的,并不可能对已经存在于服务器上的文件进行访问。
下载代码,可以参考如下:
/// <summary>
/// 下载文件
/// </summary>
/// <param name="path">文件路径</param>
public void downLoad(string path)
{
try
{
string filePath = path;
int temp = filePathLastIndexOf("/") + 1;
string fileName = filePathSubstring(temp, filePathLength - temp);
//FileStream fileStream = new FileStream(ServerMapPath(filePath), FileModeOpen, FileAccessRead, FileShareRead);
FileStream fileStream = new FileStream(filePath, FileModeOpen, FileAccessRead, FileShareRead);
long fileSize = fileStreamLength;
ContextResponseContentType = "application/octet-stream";
ContextResponseAddHeader("Content-Disposition", "attachment; filename=\"" + >
import javaawt;
import javaawtevent;
import javaxswing;
import javatextDateFormat;
import javaxswingevent;
import javaxswingtable;
import javaxswingtree;
import javaxswingfilechooser;
import javaio;
import javautil;
public class WinExplorer extends JFrame implements TreeSelectionListener{
JPanel contentPane=(JPanel)getContentPane();;
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("文件");
JMenuItem menuItemExit = new JMenuItem("退出");
JToolBar toolBar = new JToolBar();
JButton bttUp = new JButton();
Icon iconUp=UIManagergetIcon("FileChooserupFolderIcon");
JLabel statusBar = new JLabel();
DetailTable detailTable = new DetailTable();
JScrollPane sp = new JScrollPane(detailTable);
JSplitPane split = new JSplitPane();
FileSystemView fileSystemView=FileSystemViewgetFileSystemView();
FileNode root=new FileNode(fileSystemViewgetRoots()[0]);
DefaultTreeModel treeModel=new DefaultTreeModel(root);
JTree tree = new JTree(treeModel);
public static void main(String argv[]){
try {
UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName());
Font font=new Font("宋体",FontPLAIN,12);
String names[]={"Label","CheckBox","PopupMenu","TextPane",
"MenuItem","CheckBoxMenuItem","JRadioButtonMenuItem",
"ComboBox","Button","Tree","ScrollPane","TabbedPane",
"EditorPane","TitledBorder","Menu","TextArea","OptionPane",
"MenuBar","ToolBar","ToggleButton","ToolTip","ProgressBar",
"TableHeader","Panel","List","ColorChooser","PasswordField",
"TextField","Table","Label","Viewport","RadioButtonMenuItem",
"RadioButton"};
for(int i=0;i<nameslength;i++)UIManagerput(names[i]+"font",font);
UIManagerput("Labelforeground",Colorblack);
UIManagerput("Borderforeground",Colorblack);
UIManagerput("TitledBordertitleColor",Colorblack);
new WinExplorer()show();
}catch(Exception e){
eprintStackTrace();
}
}
public WinExplorer() {
contentPanesetLayout(new BorderLayout());
Dimension dimension = getToolkit()getScreenSize();
int i = (dimensionwidth - 640) / 2;
int j = (dimensionheight - 480) / 2;
setBounds(i,j,640,480);
setTitle("资源管理器");
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
setJMenuBar(menuBar);
statusBarsetText(" ");
menuItemExitaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {Systemexit(0);}
});
bttUpsetIcon(iconUp);
bttUpaddActionListener(new javaawteventActionListener() {
public void actionPerformed(ActionEvent e) {
try{
TreePath upPath=treegetSelectionPath()getParentPath();
if(upPath!=null){
treesetSelectionPath(upPath);
treescrollPathToVisible(upPath);
}
}catch(Exception ex){}
}
});
splitsetDividerSize(6);
splitsetLeftComponent(new JScrollPane(tree));
splitsetRightComponent(sp);
splitsetDividerLocation(180);
spgetViewport()setBackground(Colorwhite);
menuFileadd(menuItemExit);
menuBaradd(menuFile);
contentPaneadd(toolBar, BorderLayoutNORTH);
toolBaradd(bttUp, null);
contentPaneadd(statusBar, BorderLayoutSOUTH);
contentPaneadd(split, BorderLayoutCENTER);
treeaddTreeExpansionListener(new MyExpandsionListener());
treesetCellRenderer(new MyTreeCellRenderer());
treeaddTreeSelectionListener(this);
treesetSelectionRow(0);
treesetComponentOrientation(ComponentOrientationUNKNOWN);
}
public void valueChanged(TreeSelectionEvent e){
Object obj=treegetLastSelectedPathComponent();
if(obj==null)return;
else detailTablesetParent(((FileNode)obj)getFile());
}
class MyTreeCellRenderer extends DefaultTreeCellRenderer {
public MyTreeCellRenderer() {}
public Component getTreeCellRendererComponent(JTree tree,Object value,
boolean sel,boolean expanded,boolean leaf,int row,boolean hasFocus) {
supergetTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,hasFocus);
setIcon(fileSystemViewgetSystemIcon(((FileNode)value)getFile()));
return this;
}
}
class MyExpandsionListener implements TreeExpansionListener {
public MyExpandsionListener() {}
public void treeExpanded(TreeExpansionEvent event) {
if(eventgetPath() == null){
return;
}
treesetCursor(new Cursor(CursorWAIT_CURSOR));
TreePath path = eventgetPath();
FileNode node = (FileNode)pathgetLastPathComponent();
nodeexplore();
treeModelnodeStructureChanged(node);
treesetCursor(new Cursor(CursorDEFAULT_CURSOR));
}
public void treeCollapsed(TreeExpansionEvent event) {}
}
class FileNode extends DefaultMutableTreeNode {
private boolean explored = false;
public FileNode(File file) { setUserObject(file); }
public boolean getAllowsChildren() { return isDirectory(); }
public boolean isLeaf() { return !isDirectory();}
public File getFile() { return (File)getUserObject(); }
public boolean isExplored() { return explored; }
public void setExplored(boolean b){ explored=b;}
public boolean isDirectory() { return getFile()isDirectory();}
public String toString() {
File file = (File)getUserObject();
String filename = filetoString();
int index = filenamelastIndexOf(Fileseparator);
return (index != -1 && index != filenamelength()-1)
filenamesubstring(index+1) : filename;
}
public void explore() {
if(!isExplored()) {
File file = getFile();
File[] children = filelistFiles();
if(children==null||childrenlength==0)return;
for(int i=0; i < childrenlength; ++i)
{
File f=children[i];
if(fisDirectory())add(new FileNode(children[i]));
}
explored = true;
}
}
}
class DetailTable extends JTable{
DetailTableModel model=new DetailTableModel();
public DetailTable(){
setModel(model);
setShowGrid(false);
TableColumnModel colModel = getColumnModel();
for (int i=0;i<3;i++)
colModelgetColumn(i)setCellRenderer(new DetailsTableCellRenderer());
setRowHeight(18);
thisaddMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
if(egetClickCount()==2){
for (int i = 0; i <getRowCount(); i++) {
if(getCellRect(i,0,true)contains(egetPoint())){
openSelect();
break;
}
}
}
}
});
}
public void openSelect(){
Object obj=modelgetValueAt(getSelectedRow(),0);
if(obj==null)return;
File f=(File)obj;
if(fisDirectory()){
//expand tree
}else{
//open select file
}
}
public void setParent(File parent){
modelremoveAllRows();
File list[]=parentlistFiles();
if(list==null)return;
Vector vDir=new Vector(),vFile=new Vector();
for (int i = 0; i < listlength; i++) {
if(list[i]isDirectory())vDiradd(list[i]);
else vFileadd(list[i]);
}
sortElements(vFile);
sortElements(vDir);
for (int i = 0; i < vDirsize(); i++)modeladdFile((File)vDirelementAt(i));
for (int i = 0; i < vFilesize(); i++)modeladdFile((File)vFileelementAt(i));
}
public void sortElements(Vector v)
{
for(int i=0;i<vsize();i++)
{
int k=i;
for(int j=i+1;j<vsize();j++){
File fa=(File)velementAt(j);
File fb=(File)velementAt(k);
if(fileSystemViewgetSystemDisplayName(fa)toLowerCase()compareTo(
fileSystemViewgetSystemDisplayName(fb)toLowerCase())<0)k=j;
}
if(k!=i)swap(k,i,v);
}
}
private void swap(int loc1,int loc2,Vector v){
Object tmp=velementAt(loc1);
vsetElementAt(velementAt(loc2),loc1);
vsetElementAt(tmp,loc2);
}
class DetailTableModel extends DefaultTableModel {
public DetailTableModel() {
addColumn("名称");
addColumn("大小");
addColumn("修改时间");
}
public void addFile(File f){
addRow(new Object[]{f,new Double(flength()/1024),
new javasqlDate(flastModified())});
}
public void removeAllRows(){
while(getRowCount()!=0) removeRow(0);
}
public boolean isCellEditable(int row, int column) {return false;}
}
class DetailsTableCellRenderer extends DefaultTableCellRenderer {
DetailsTableCellRenderer() {}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (column == 1){
setHorizontalAlignment(SwingConstantsTRAILING);
isSelected=hasFocus=false;
}else if(column==2){
setHorizontalAlignment(SwingConstantsCENTER);
isSelected=hasFocus=false;
}else setHorizontalAlignment(SwingConstantsLEADING);
return supergetTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
}
public void setValue(Object value) {
setIcon(null);
if (value instanceof File) {
File file = (File)value;
setText(fileSystemViewgetSystemDisplayName(file));
setIcon(fileSystemViewgetSystemIcon(file));
}else{
supersetValue(value);
}
}
}
}
}
你双击左面的桌面图标就可以浏览了,别双击字。
string url =">
以上就是关于一个处理文本数据的python脚本或者java小程序全部的内容,包括:一个处理文本数据的python脚本或者java小程序、java怎么取得倒数第二个“/”后面的、asp.net小程序FileUpload控件无法实现文件下载。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)