从文本文件中读取,创建另一个文件

时间:2017-01-16 10:48:54

标签: java

当我从.txt文件中读取时,它正在制作.txt文件的另一个副本,并在名称中添加“.txt”。

例如:从“hello.txt”读取并创建“hello.txt.txt”..我发现问题是包含FileWriter flwrtr = new FileWriter(fl.getPath()+".txt");的行但是如果我删除字符串则不起作用有谁知道解决方案?

  String path="";

  JFileChooser fileopenchooser = new JFileChooser();
  fileopenchooser.setDialogTitle("Open Quiz");
  FileNameExtensionFilter filter = new FileNameExtensionFilter("Text File", "txt");
  fileopenchooser.setFileFilter(filter);

  int getvlue = fileopenchooser.showOpenDialog(fileopenchooser);
  if(getvlue == JFileChooser.APPROVE_OPTION){

      File fl = fileopenchooser.getSelectedFile();
      try{

        FileWriter flwrtr = new FileWriter(fl.getPath()+".txt");
        path = fl.getPath();
        flwrtr.close();

      }
      catch(Exception e){
        JOptionPane.showMessageDialog(null,"Problem Saving File!","ERROR",JOptionPane.WARNING_MESSAGE);
      }

3 个答案:

答案 0 :(得分:1)

这是Java,而不是C.您会收到一个新文件“hello.txt.txt”,因为您在新的FileWriter调用中添加了“.txt”。你说你想要读取文件,那么为什么要创建一个FileWriter,用于写入文件而不是读取。如果要阅读,请使用FileReader。

答案 1 :(得分:0)

您是否注意到getPath方法是返回文件的路径,包括文件名?我知道这可能会产生误导,这就是为什么你有2个txt。也许你应该为路径做一些字符串操作,比如减少3个字符等。

例如:Dim Src As Worksheet ' sets range from Set Src = ThisWorkbook.Sheets("ANAF ANGAJATORI") ' finds the last row with data in column A lastRow = Src.range("A" & Src.Rows.Count).End(xlUp).Row ' removes duplicates Sheets("ANAF ANGAJATORI").range("A2:F" & lastRow).RemoveDuplicates Columns:=Array(1, 3), Header:=xlYes ' formats the cells again to previous state by copying formatting from a certain cell (in this exemple A20000 is my last cell in the sheet, it will never be filled with data inputed by user) Sheets("ANAF ANGAJATORI").range("A20000").Copy Sheets("ANAF ANGAJATORI").range("A3:B" & lastRow).PasteSpecial xlPasteFormats Sheets("ANAF ANGAJATORI").range("A20000").Copy Sheets("ANAF ANGAJATORI").range("E3:F" & lastRow).PasteSpecial xlPasteFormats Sheets("ANAF ANGAJATORI").range("C20000").Copy Sheets("ANAF ANGAJATORI").range("C3:D" & lastRow).PasteSpecial xlPasteFormats

答案 2 :(得分:0)

再次,我一直在想,......

更好的是,如果您在测验系统中瞄准测验文档,该测验系统打开为测验参与者分发的文件,然后他们打开它,那么他们可以填写表格(例如Pdf表格)并回答并签名(或自动填写)签名作为你的登录)并提交/保存它,从而使系统有所有有效/合法测试/测验文件的文件,可以查询丹判断/等级,我认为这将是如何:

型号:

$img = "http://image-generated-online/";
$description = "An image";

$sql->prepare('INSERT INTO table(img, desc) VALUES (?,?)');
$sql->bind_param("attr-for-image, s", $img, $description);

$sql->execute();

和tcompoent的接口

MEDIUMBLOB

SEEE:

在这里你可以关联JquisFileCooser的compojnet(包括注入界面,例如使用spring例如。

(虽然可以jsut使用bean进行属性约束)

然后在你的主要代码上

package com.emerlard.test.temp.test.model;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 *
 * @author eddhie
 */
@Entity
public class Document implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Document)) {
            return false;
        }
        Document other = (Document) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.emerlard.test.temp.test.model.Document[ id=" + id + " ]";
    }

    private String Name;

    private User CreatedBy;


    //todo:create a directory system/model
    private Directory directory;

    `


}-----

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.emerlard.test.temp.test.model;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 *
 * @author eddie
 */
@Entity
public class QuestionDocument extends Document implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof QuestionDocument)) {
            return false;
        }
        QuestionDocument other = (QuestionDocument) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.emerlard.test.temp.test.model.QuestionDocument[ id=" + id + " ]";
    }





} /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.emerlard.test.temp;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author eddhie
 * 
 */
public class JQuizFileChooser extends JFileChooser implements IJQuizFileChooser   {
    //todo:the event still using property change call so maybe need to be regular agent or not. but it is quite standard for this java beans
    public static final String PROP_FILE_CHOOSEN_EVENT = "FileChoosenEvent";

    private String FileChoosenEvent;

    private PropertyChangeSupport propertySupport;


    @Override
    public String getFileChoosenEvent() {
        return FileChoosenEvent;
    }

    @Override
    public void setFileChoosenEvent(String value) {
        String oldValue = FileChoosenEvent;
        FileChoosenEvent = value;
        propertySupport.firePropertyChange(PROP_FILE_CHOOSEN_EVENT, oldValue, FileChoosenEvent);
    }

    //todo:what to do woith mutltipel file seleantion
    @Override
    public void setSelectedFile(File file) {
        super.setSelectedFile(file); //To change body of generated methods, choose Tools | Templates.
        //todo:what aobut mamignt eh proeety hcangei envet is not sring but drectoyr fpeorty
        setFileChoosenEvent("Selected File Changed , do your setting of your hander to fill the containter");

    }



    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.removePropertyChangeListener(listener);
    }

    public JQuizFileChooser() {
                propertySupport = new PropertyChangeSupport(this);
        this.setFileFilter(new FileNameExtensionFilter("Text File", "txt"));

    }



}

这将导致此filechooser或句柄的内容

然后你可以在主代码中捕获filecchoosen的事件,就像是

package com.emerlard.test.temp;

/**
 *
 * @author eddhie
 */
public interface IJQuizFileChooser {

    String getFileChoosenEvent();

    void setFileChoosenEvent(String value);

}

evt与您的活动相对应的地方

中填充显示容器

在保存时,您使用一些文件写入器以正确的ID名称

写入正确的目录

您只需连接模型记录以获得简单的应用程序模型

GOTIT?

所以最终你只需要所有这些

@autowire Iquizzfilechooser

@autowire Containner //如果你想这样做的话 你可以根据你想要的东西立即运行,如果,其他是注射或者如果你想添加更多其他东西由你决定

很酷吧?